Using client tasks using WCFFacility in Castle.Windsor

I would like to take advantage of the new tasks for the WCF client. I am currently using WCFFacility as follows:

container.Register(Component .For<IAdminService>() .LifeStyle.Transient .AsWcfClient(new DefaultClientModel() { Endpoint = WCFHelpers.BasicHttp(settings.MaxReceivedMessageSize) .At(addr) })); 

where IAdminService is the ServiceContract class. All MSDN articles about tasks based on tasks relate to setting the checkbox “tasks based on operations” when importing a service link. But in the style I'm currently using, there is no reference to the imported service, because I just refer directly to the service contract interface.

So, I am wondering how I can enable support for operations with tasks with a minimal number of changes in the current code.

[BTW - WCFHelpers is a utility class that generates a BindEndpointModel, and addr is set to the corresponding endpoint address before this code is executed]

+2
source share
1 answer

WCFFacility provides some extension methods that match the old async pattern. They can easily be converted to Tasks.

Try these extension methods:

 public static class ClientExtensions { public static async Task<TResult> CallAsync<TProxy, TResult>(this TProxy proxy, Func<TProxy, TResult> method) { return await Task.Factory.FromAsync(proxy.BeginWcfCall(method), ar => proxy.EndWcfCall<TResult>(ar)); } public static async Task CallAsync<TProxy>(this TProxy proxy, Action<TProxy> method) { await Task.Factory.FromAsync(proxy.BeginWcfCall(method), ar => proxy.EndWcfCall(ar)); } } 

In the asynchronous method, they can be called like this:

 // Func<T> var result = await client.CallAsync(p => p.SayThisNumber(42)); // Action await client.CallAsync(p => p.DoSomething()); 
+2
source

All Articles