How to get data (asynchronously) from WCF?

I have a code that uses WCF, requests data from a server.

Example:

public static Company LoadCompanyInfo(Guid session) { var client = new QualerServiceClient("QualerService"); return client.GetCompanyInfo(session); } 

I need to get my wpf application to run this code asynchronously.

I'm trying to:

  public static Company LoadCompanyInfoAsync(Guid session) { var client = new QualerServiceClient("QualerService"); client.BeginGetCompanyInfo(session, new AsyncCallback(EndLoadCompanyInfoAsync), client); // How to Get Data from AsyncCallback function? return null; } private static void EndLoadCompanyInfoAsync(IAsyncResult r) { var client = r.AsyncState as QualerServiceClient; var result = client.EndGetCompanyInfo(r); // how to return the result value ?? } 

But I do not know how to return data from the callback function.

I have methods:

  • BeginGetCompanyInfo and EndGetCompanyInfo

  • GetCompanyInfoAsync

and event:

  • GetCompanyInfoCompleted.

Quastions

  • How can I get data from the callback method?

  • What is the difference between GetCompanyInfoAsync and Begin\End ?

  • Recommendations: how can I execute the method asynchronously so that the graphical interface of my WPF application does not hang?

+4
source share
3 answers

I assume you are using VS2012.

First, if your goal is .NET 4.0, then install (via NuGet) the Async Targeting package. If your goal is .NET 4.5, you don’t have to do anything special.

Then recreate your client proxy. Existing Begin / End / Async endpoints will be replaced with one Task<Company> GetCompanyInfoAsync(Guid) .

Now you can use it as follows:

 public static Task<Company> LoadCompanyInfoAsync(Guid session) { var client = new QualerServiceClient("QualerService"); return client.GetCompanyInfoAsync(session); } public async void ButtonClickOrWhatever(...) { var company = await LoadCompanyInfoAsync(mySession); // Update UI with company info. } 

The old pair of Begin / End methods used the asynchronous programming model (APM). The old Async / event pair used an event -based asynchronous programming (EAP) model. The new Async method uses a task-based asynchronous programming (TAP) model. I have more information about Async WCF on my blog.

+3
source

You can use async CTP for .net 4, which will become part of .net in 5.

It adds two new keywords in C # async and is waiting for keywords.
You mark a method or lamda with the async keyword and return Task or Task<T>

when you call your method, you name it like that

 var result = await LoadCompanyInfo(sessionId); 

Async ctp

  public static async Task<Company> LoadCompanyInfo(Guid session) { Company result = default(Company); await TaskEx.Run(()=>{ var client = new QualerServiceClient("QualerService"); result = client.GetCompanyInfo(session); }); return result; } 
0
source

Use TPL :

 Task t = TaskFactory.FromAsync( beginMethod: BeginGetCompanyInfo, endMethod: EndGetCompanyInfo, arg1: session, state: null); t.ContinueWith(result => { // handle result }); 
0
source

All Articles