问题描述
我想利用新的基于任务的作战中WCF客户端。我目前使用的WCFFacility如下:
I would like to take advantage of the new task-based operations for a WCF client. I am currently using the WCFFacility as follows:
container.Register(Component
.For<IAdminService>()
.LifeStyle.Transient
.AsWcfClient(new DefaultClientModel()
{
Endpoint = WCFHelpers.BasicHttp(settings.MaxReceivedMessageSize)
.At(addr)
}));
其中IAdminService是的ServiceContract类。所有关于基于任务的操作MSDN文章引用导入服务引用时,设置一个基于任务的操作复选框。但在我目前使用的风格,也没有进口服务引用,因为我简单直接引用服务合同接口。
where IAdminService is the ServiceContract class. All the MSDN articles about task-based operations refer to setting a 'task based operations' tick box when importing the service reference. But in the style I am currently using, there is no imported service reference because I simple refer directly to the service contract interface.
所以我想知道怎样才能实现与改变目前的code量最少的基于任务的操作提供支持。
So I am wondering how I can enable support for task-based operations with the least amount of changes to the current code.
[顺便说一句 - WCFHelpers是产生BindEndpointModel和地址一个实用工具类设置为在此之前,code正在执行一个适当的端点地址]
[BTW - WCFHelpers is a utility class that generates a BindEndpointModel and addr is set up to an appropriate endpoint address prior to this code being executed]
推荐答案
该WCFFacility提供符合老异步模式的一些推广方法。这些可以很容易地转换成任务。
The WCFFacility provides some extension methods that conform to the old async pattern. These can easily be converted to Tasks.
尝试这些扩展方法:
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 an async 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());
这篇关于在Castle.Windsor使用基于任务的客户端操作与WCFFacility的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!