WCF 测试客户端中的 Async() 方法

use*_*809 5 c# testing wcf client server

我写了一个简单的 WCF 服务,当我调试项目时,我得到一个 WCF 客户端窗口,其中每个服务方法都有一个 async() 版本(例如,对于来自服务的方法 ConnectMessages() 有一个新方法 GetMessagesAsync() . 但是,异步方法被隐藏起来并用红色“x”标记,并带有以下标题:

wcf 测试客户端不支持此操作,因为它使用 system.threading.tasks.task

我的问题是:为什么每个方法都有一个异步版本,为什么这些异步被标记为不起作用?这是什么意思?

Ehs*_*jad -1

当我们通常调用服务方法时,我们的代码会顺序执行,等待服务返回的响应,并且代码执行被阻止,当我们使用异步方法时,我们的代码不会阻塞并继续执行,服务返回时会触发一个事件回复。

如果我同步调用服务:

var result = ReportClientObj.GetUserCoordinatesReport(searchParams);
  if(result == null)  // this line will not ewxecute until above line of code executes and completes
  {
   // do something
  }
Run Code Online (Sandbox Code Playgroud)

如果我使用异步代码执行将不会停止:

var result = ReportClientObj.GetUserCoordinatesReportAsync(searchParams);
  if(result == null)  // this line will execute and will not wait for above call to complete due to asynshronous call and this will bang
  {
   // do something
  }
Run Code Online (Sandbox Code Playgroud)

为了在异步中执行上述操作,我们必须使用其 Completed 事件

注册它的事件:

    reportClient.GetUserCoordinatesReportCompleted += reportClient_GetUserCoordinatesReportCompleted;
Run Code Online (Sandbox Code Playgroud)

然后捕获已完成的事件,使用其响应:

void reportClient_GetUserCoordinatesReportCompleted(object sender, GetUserCoordinatesReportCompletedEventArgs e)
{
    // Use Result here 
  var Result = e.Result;
  if(Result !=null)
  {
    // do something
  }
}
Run Code Online (Sandbox Code Playgroud)

看这里 :

http://msdn.microsoft.com/en-us/library/ms730059%28v=vs.110%29.aspx

http://www.codeguru.com/columns/experts/building-and-consuming-async-wcf-services-in-.net-framework-4.5.htm