异步编程模型 (APM) 是否在 WCF 操作期间阻塞线程



我正在开发一个高度线程化的应用程序,我们的服务器调用正在使用使用 APM 模式的 WCF。

我们不会使用 .Net 4.5.1,所以我不能将async/await与 TPL 一起使用。我最终试图弄清楚仍然使用 TPL(带 Task.Factory.FromAsync() )是否比 APM 更有益。

APM 模式在等待从网络从 WCF 操作返回时是否会阻止线程?

编辑:代码示例

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);
}

对于您提供的代码示例,它不会阻塞任何线程。可能阻止的情况是,如果您这样做

public void DoSomething()
{
  IWcfServiceAgent agent = new WcfServiceAgentProxy();
  var request = new DoSomethingRequest();
  var iar = agent.BeginDoSomething(request, null, null);
  //Do some other time consuming work that does not depend on the response.    
  var response = agent.EndDoSomething(iar); //This blocks till DoSomething completes.
  //Do something with the response.
}

最新更新