简体   繁体   中英

Async() methods in WCF test client

I wrote a simple WCF service and when I debug the project I get a WCF client window in which each service method has an async() version (for example, for the method ConnectMessages() from the service there is a new method GetMessagesAsync(). However, the async methods are shadowed and marked with a red 'x' and ahve the following caption:

This operation is not supported in wcf test client because it uses system.threading.tasks.task

my questions are: why does each method have an async version and why are these async marked as unfunctional? what does this mean?

When we normally call a service method our code is executing sequentially ans it waits for the response returned by service and code execution is blocked, when we use Async methods, our code does not blocks and continues its execution and services fires an event when it returns response.

If i have called service synchronously:

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

if i use async code execution will not stop:

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
  }

For doing above thing in Async we have to use its Completed event

Register it's event:

    reportClient.GetUserCoordinatesReportCompleted += reportClient_GetUserCoordinatesReportCompleted;

and then catch the comleted event, use its reponse:

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

See here :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM