Reputation: 9578
I have a Silverlight 5 app, that makes use of a WCF service. The proxy client that has been generated has only asychronous methods (by default, when generating from the SL client).
I want to make use of the Task-based Asynchronous Pattern (TAP), now within VS2012RC.
What is the best approach to consume the async methods from the generated client proxy ?
(the issue is, that the WCF proxy generator creates code that is based on the Event-based Asynchronous Pattern (EAP) and not TAP....)
Upvotes: 0
Views: 1116
Reputation: 127603
I don't know if it was available in the RC, however as of the SDK 8.0A (the one included with VS2012) svcutil.exe
will generate async methods using the TAP pattern.
It will use TAP by default so be sure to NOT include /async
as that will make it fall back to the old APM method of generating the methods.
You can see if the version of svcutil
is new enough to use the TAP by looking at the first lines of the program it will include that it is at least version 4.0 of the tool.
Microsoft (R) Service Model Metadata Tool [Microsoft (R) Windows (R)
Communication Foundation, Version 4.0.xxxxx.xxxxxx]
Upvotes: 0
Reputation: 9578
Based on this document: http://www.microsoft.com/en-us/download/details.aspx?id=19957
I have found a solution for this.
See code below:
public class MyDataListProvider : IMyDataListProvider
{
private <ObservableCollection<IMyData>> myDataList;
public Task<ObservableCollection<IMyData>> GetMyData()
{
TaskCompletionSource<ObservableCollection<IMyData>> taskCompletionSource = new TaskCompletionSource<ObservableCollection<IMyData>>();
MyWCFClientProxy client = new MyWCFClientProxy();
this.myDataList.Clear();
client.GetMyDataCompleted += (o, e) =>
{
if (e.Error != null)
{
taskCompletionSource.TrySetException(e.Error);
}
else
{
if (e.Cancelled)
{
taskCompletionSource.TrySetCanceled();
}
else
{
foreach (var s in e.Result)
{
var item = new MyData();
item.Name = s.Name;
item.Fullname = s.Fullname;
this.myDataList.Add(item);
}
taskCompletionSource.TrySetResult(this.myDataList);
}
}
};
client.GetMyDataAsync();
return taskCompletionSource.Task;
}
}
Client SL code:
private async void SetMyDataList()
{
this.MyDataList = await this.myDataListProvider.GetMyData();
}
Upvotes: 2