Asynchronous WCF calls without a generated proxy.

Like many other developers, I like to have control over the code I deploy. To enable asynchronous calls you normally have to generate the proxies with the option for asynchronous calls turned on. In the case where you have your own proxies; the ability to enable asynch calls is pretty simple.

You’ll likely have your standard synchronous interface as follows:

[ServiceContract]
public interface IFileService
{
[OperationContract]
FileResults GetFileConfig(FileRequest request);

}

To provide an asynchronous version simple create another interface; using the name alias against the service contract and asyncpattern set to true. Create a Begin and End pair of functions, post appended with the name of the operation you want to make synchronous.

[ServiceContract(Name=”IFileService”)]
public interface IAsyncFileService
{
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginGetFileConfig(FileRequest request, AysncCallback callback, object asyncState);

LoadConfigResults EndGetFileConfig(IAsyncResult result);

}

 

Now all your client has to do is is use the new Async interface and the server will resolve to an asynchronous operation.

public class FileServiceProxy : ClientBas<IAsyncFileService>, IAsyncFileService
{
IAsyncResult BeginGetFileConfig(FileRequest request, AysncCallback callback, object asyncState)
{
return Channel.BeginGetFileConfig(request, callback, asyncState);
}

…… // and the End call equivalent

}

Easy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s