WCF的ChannelFactory异步调用 [英] WCF ChannelFactory asynchronous call
问题描述
我与WCF和TPL异步库打
我需要的是能够多申请WCF的方法和等待,直到一切都将结束,到目前为止,我发现,在.NET 4.5有非常方便的方法 Task.Factory.ContinueWhenAll 可用于等到所有呼叫都完成
I am playing with WCF and TPL Async library What I need is to be able request multiple WCF methods and wait untill all will be finished, so far I found that in .NET 4.5 there is very handy method Task.Factory.ContinueWhenAll which can be used to wait until all calls are finished
我发现如下因素的方式来要求以异步方式结果调用WCF
选项1.通过使用由添加引用对话框,选择生成的代理生成基于任务的操作 - > [如:这里] [1] - 在没有我的情况下一种选择,因为我们使用的是原始的ChannelFactory结果
选项2.在任务例如包装同步调用。
I found folowing ways to request WCF call in asynchronous way
Option 1. By using a proxy generated by "Add reference" dialog with option "Generate task-based operations" -> [e.g. here][1] - not an option in my case as we are using raw ChannelFactory
Option 2. By wrapping synchronous call in a task e.g.
ChannelFactory<IService1> factory = new ChannelFactory<IService1>("BasicHttpBinding_IService1");
Task<string> t1 = Task<string>.Factory.StartNew(() => { return factory.CreateChannel().GetData(2); });
Task<string> t2 = Task<string>.Factory.StartNew(() => { return factory.CreateChannel().GetData(5); });
Task.Factory.ContinueWhenAll(new[] { t1, t2 }, t =>
{
foreach (var task in t)
{
//get result here
}
});
选项3.通过创建合同接口例如的客户端异步版本。
Option 3. By creating client side asynchronous version of contract interface e.g.
[ServiceContract(Namespace = "X", Name = "TheContract")]//Server side contract
public interface IService1
{
[OperationContract]
string GetData(int value);
}
[ServiceContract(Namespace = "X", Name = "TheContract")]//client side contract
public interface IService1Async
{
[OperationContract]
string GetData(int value);
[OperationContract]
Task<string> GetDataAsync(int value);
}
和有这个我可以调用方法异步例如。
And having this I can invoke method asynchronously e.g.
ChannelFactory<IService1Async> factory = new ChannelFactory<IService1Async>("BasicHttpBinding_IService2");
var t1 = factory.CreateChannel().GetDataAsync(2);
var t2 = factory.CreateChannel().GetDataAsync(5);
Task.Factory.ContinueWhenAll(new[] { t1, t2 }, (Task<string>[] t) =>
{
foreach (var task in t)
{
//get result here
}
});
所以,问题是如下,有什么优势已经使用选项2比较选项3,呼吁WCF方法,在方案2是正确的?选项2具有在比较中的优点与3即没有必要创建客户端合同接口
So the question is as follows, what advantages has option 3 in comparison with option 2, is calling WCF methods as in Option 2 is correct ? Option 2 has one advantages in comparison with 3 namely there is no need to create client side contract interface.
推荐答案
在选项#2,每次调用的GetData()
将阻止整个时间线程方法执行。在选项#3,而 GetDataAsync()
的操作过程中,没有线程被阻塞它
In option #2, each invocation of GetData()
will block a thread for the whole time the method executes. In option #3, while the GetDataAsync()
operation is in progress, no thread is blocked by it.
这意味着,选项#3是更有效率,如果效率对你很重要,你应该使用方法 -async
版本。
This means that option #3 is more efficient and you should use the -Async
version of the method if efficiency is important for you.
这篇关于WCF的ChannelFactory异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!