If I call a WCF service method I would do something like this:
proxy.DoSomethingAsync();
proxy.DoSomethingAsyncCompleted += OnDoSomethingAsyncCompleted;
How could I do the same using the new async
ctp?
I guess I would need something like proxy.DoSomethingTaskAsync
or proxy.DoSomethingAsync().ToTask()
? The web service call needs to return a Task<T>
to be able to use the await
keyword, but how??
An asynchronous service using async-await is very responsive as it can interleave many client calls and execute them in parallel (2). Despite this, the service can run fully thread-safe on one thread (3) and can be a singleton service (1) or a service object created by the framework for a session or a call only.
When implementing the service, please note the ServiceBehaviourAttributes (1)...(3) :
To run every call on one thread, a System.Threading.SynchronizationContext.Current != null must be present at the moment you Open() the ServiceHost. Using the SynchronizationContext, you need not to care about locks. Atomic, non interruptable code sections stretch roughly from one await to the next. Take care that shared service data is in a consistent state at every await and be aware that successive requests from one client may be responded not in the order they where sent.
On client side, the asynchronous service operation is awaitable:
It is not possible to use out or ref parameters in the operation contract. All response data must be passed by the returned value Task(T).
I use this interface in AsyncWcfLib, it supports a Actor based programming model.
You rightly point out that async/await are built around methods returning Task and Task (for a great explanation of the working of async/await, see here). Here's how to generate Task based methods for a WCF service:
Visual Studio 2012 RC has an additional check-box in the "Configure Service Reference" dialog box: "Generate task based operations". While I don't see that option documented in the MSDN, I expect it exists specifically to allow seamless async/await on WCF calls.
For earlier versions, take a look at this post, describing an extension which allows generating Task<> based methods on WCF even with the CTP.
It is quite common to have asynchronous clients calling a synchronous service.
The following client and service contracts match (a bit magic used behind the scenes):
The client interface is directly awaitable:
It is not possible to use out or ref parameters in the operaton contract. All response data must be passed in the return value. This actually was a breaking change for me.
I use this interface in AsyncWcfLib, it supports a Actor based programming model.
In the CTP there are factory methods that do the work of turning regular APM functions (Begin/End) into ones that are compatible with the new async keyword, for instance:
So in your case you can do the equivalent and then you'd then call it like so:
See this thread on the CTP discussion group for more info
There is a WCF sample in the Async CTP that will show you how to use the async/await model in WCF.
In terms of plans for supporting this model in WCF, you can take a look at the following post:
http://blogs.msdn.com/b/endpoint/archive/2010/11/13/simplified-asynchronous-programming-model-in-wcf-with-async-await.aspx
Hope this helps.
Amadeo
As mentioned by Matt, there is a
TaskFactory.FromAsync
method that allows you to create aTask
from aBegin
/End
pair. You need to enable asynchronous endpoints when you add your WCF reference, and then you can wrap them up yourself using extension methods.As mentioned by Amadeo, there is a sample of this in the Async CTP, under the
(C# WCF) Stock Quotes
directory.Also in that directory, there is a
TaskWsdlImportExtension
project. Add a reference to that dll and modify your .config as such:Then you don't have to do your own wrapping at all; the
TaskWsdlImportExtension
will do it for you.