在WCF操作使用异步编程模型(APM),这是怎么回事"引擎盖&QUOT下;? [英] Using the Asynchronous Programming Model (APM) in a WCF operation, what's going on "under the hood"?

查看:138
本文介绍了在WCF操作使用异步编程模型(APM),这是怎么回事"引擎盖&QUOT下;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的操作,如本

 公共无效DoSomething的()
{
  IWcfServiceAgentAsync剂=新WcfServiceAgentProxy();  VAR要求=新DoSomethingRequest();  agent.BeginDoSomething(要求,
    IAR =>
    {
      VAR响应= agent.EndDoSomething(IAR);      / *
       *元帅回用效果UI线程
       * /
    }, 空值);
}

什么是的真的正在进行该操作被启动,并执行回调的时刻之间的引擎盖底下?有没有变得越来越等待调查完成一个插座?是否有被封锁,直到它返回一个底层操作系统的线程?


解决方案

什么情况是 BeginDoSomething 结束调用 base.Channel.BeginGetTest(回调,asyncState); 的WCF代理。那么,什么是代理做的就​​是去通过绑定堆栈你已经设定了您的WCF通信的每个部分。

一个绑定堆栈您的申请将通过的主要部分是的消息恩codeR 。 EN codeR包你的请求起来的东西,可以重新psented为字节[] (这个过程称为序列化)。

一旦通过消息EN codeR您的请求将会被发送到的交通运输(无论是的 HTTP ,的 TCP ,或别的东西)。运输取字节[] ,并将其发送到目标端点,它然后告诉OS当你收到定向到我的响应,调用这个函数通过< A HREF =htt​​p://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx相对=nofollow> IO完成端口系统。 (我将承担TCP或HTTP对这个答案的其余部分结合)(修改:注意,IO完成端口没有被使用,它是由传输层来决定什么不,它只是大多数内置于框架实现将使用)

在你的信息之间的时间间隔发送和收到答复没有线程,没有极化,什么都没有发生。当网卡收到响应它提出了一个中断,告诉它有新的信息操作系统,该中断处理并最终OS发现它是用于应用程序数据的几个字节。然后,OS告诉你的应用程序启动一个IOCP线程池线程并为其传递一个接收数​​据的几个字节。

(请参阅没有线程斯蒂芬·克利里有关此过程的详细信息,这是在谈论TPM,而不是像APM在你的榜样,但下层是完全一样的。)

当接收到来自其他计算机的字节,这些字节回去了在相反方向堆叠。该IOCP线程运行从交通运输功能,它需要传递给它的字节数,并把它关闭的消息恩$ C $铬。这个动作可以发生数次!

连接codeR的消息从传输接收的字节数,并尝试建立一个消息,如果没有足够的字节已收到但它只是排队他们和下一集要传递的字节等待。一旦它有足够的字节desearalize它会创建一个新的响应对象的信息,并将其设置为的IAsyncResult ,然后(我不知道结果是谁,它可能是WCF调用堆栈,它可能会在.NET别的地方)看到你的的IAsyncResult 有一个回调委托,开始了另一个IOCP线程和线程是线程的委托正在运行。

Given an operation such as this:

public void DoSomething()
{
  IWcfServiceAgentAsync agent = new WcfServiceAgentProxy();

  var request = new DoSomethingRequest();

  agent.BeginDoSomething(request,
    iar =>
    { 
      var response = agent.EndDoSomething(iar);

      /*
       * Marshal back on to UI thread with results
       */
    }, null);
}

What is really going on underneath the hood between the moment that the operation gets started, and the callback is executed? Is there a socket that's getting polled waiting for completion? Is there an underlying OS thread that gets blocked until it's return?

解决方案

What happens is BeginDoSomething ends up calling base.Channel.BeginGetTest(callback, asyncState); on the WCF proxy. What that proxy then does is go through each part of the "Binding Stack" you have set up for your WCF communication.

One of the main parts of the binding stack your request will pass through is the "Message Encoder". The message encoder packages your request up in to something that can be represented as a byte[] (This process is called Serializing).

Once through the message encoder your request will be sent to the Transport (be it HTTP, TCP, or something else). The transport takes the byte[] and sends it to your target endpoint, it then tells the OS "When you receive a response directed to me, call this function" via the IO Completion Ports system. (I will assume either TCP or HTTP binding for the rest of this answer) (EDIT: Note, IO Completion ports don't have to be used, it is up to the Transport layer to decide what to do, it is just most of the implementations built in to the framework will use that)

In the time between your message was sent and the response was received no threads, no poling, no nothing happens. When the network card receives a response it raises a interrupt telling the OS it has new information, that interrupt is processed and eventually the OS sees that it is a few bytes of data intended for your application. The OS then tells your application to start up a IOCP thread pool thread and passes it the few bytes of data that was received.

(See "There is no Thread" by Stephen Cleary for more info about this processes. It is talking about TPM instead of APM like in your example but the underlying layers are exactly the same.)

When the bytes from the other computer are received those bytes go back up the stack in the opposite direction. The IOCP thread runs a function from the Transport, it takes the bytes that was passed to it and hands it off to the Message Encoder. This action can happen several times!

The message encoder receives the bytes from the transport and tries to build up a message, if not enough bytes have been received yet it just queues them and waits for the next set of bytes to be passed in. Once it has enough bytes to desearalize the message it will create a new "Response" object and set it as the result of the IAsyncResult, then (I am not sure who, it may be the WCF call stack, it may be somewhere else in .NET) sees that your IAsyncResult had a callback delegate and starts up another IOCP thread and that thread is the thread your delegate is run on.

这篇关于在WCF操作使用异步编程模型(APM),这是怎么回事&QUOT;引擎盖&QUOT下;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆