[英]Should Task-based WCF calls be OneWay?
A WCF contract might look like this: WCF合同可能如下所示:
[ServiceContract]
public interface IService
{
[OperationContract]
void DoSomethingFast();
[OperationContract]
void DoSomethingSlow();
[OperationContract]
int GetSomethingFast();
[OperationContract]
int GetSomethingSlow();
}
When a client makes any of these calls, its thread blocks while the server does its thing. 当客户端进行这些调用中的任何一个时,服务器执行其操作时其线程都会阻塞。 For the Fast
methods, that's no problem, but for the Slow
methods, this blocking is far from ideal. 对于Fast
方法,这没问题,但是对于Slow
方法,这种阻塞远非理想。 We can add (IsOneWay = true)
to the DoSomething
methods, but we can't do the same for the GetSomething
methods, because they really are two way - we have to wait for the result: 我们可以将(IsOneWay = true)
添加到DoSomething
方法中,但是对于GetSomething
方法我们不能做同样的GetSomething
,因为它们确实是两种方式-我们必须等待结果:
[ServiceContract]
public interface IService
{
[OperationContract(IsOneWay = true)]
void DoSomethingFast();
[OperationContract(IsOneWay = true)]
void DoSomethingSlow();
[OperationContract]
int GetSomething();
[OperationContract]
int GetSomethingSlow();
}
Enter the Task-based Asynchronous Pattern (TAP). 输入基于任务的异步模式(TAP)。 We define the contract to return Task
, and WCF (to the best of my knowledge), does some magic behind the scenes and immediately returns a Task
which waits for a callback from the WCF service before completing. 我们定义合同以返回Task
,并且WCF(据我所知)在幕后做了一些魔术,然后立即返回Task
,该Task
等待WCF服务的回调再完成。 This eliminates the client-side blocking, allowing the use of await
instead: 这消除了客户端的阻塞,允许使用await
代替:
[ServiceContract]
public interface IService
{
[OperationContract(IsOneWay = true)]
Task DoSomethingFastAsync();
[OperationContract(IsOneWay = true)]
Task DoSomethingSlowAsync();
[OperationContract]
Task<int> GetSomethingAsync();
[OperationContract]
Task<int> GetSomethingSlowAsync();
}
My question is about how this all works, and whether you still need to include (IsOneWay = true)
on the service contract: 我的问题是有关这一切的工作方式,以及是否仍需要在服务合同中包含(IsOneWay = true)
:
Task
which completes when the service tells us it's finished? WCF是否在服务告诉我们完成时自动返回完成的Task
? (IsOneWay = true)
necessary for the above magic to happen? (IsOneWay = true)
需要上述魔术才能发生? If not, does it have any effect at all? 如果没有,那有什么作用吗? DoSomething
and GetSomething
calls be treated equally? 假设来自服务的回调可以轻松完成“我已经完成”或“这里是您的结果”,那么DoSomething
和GetSomething
调用是否可以被同等对待? So if (IsOneWay = true)
is necessary, can it also be used on the GetSomething
methods? 因此,如果有必要(IsOneWay = true)
,是否还可以在GetSomething
方法上使用它? If the service is one way or not has nothing to do with the technology of the client. 服务是否是一种方式与客户的技术无关。 In C# method semantics, it just means that it's a void
returning method. 在C#方法语义中,这仅意味着它是一个void
返回方法。
Now, if the client is implemented in .NET and the user chooses to call it on a blocking way or not, it's her/his choice. 现在,如果客户端是在.NET中实现的,并且用户选择是否以阻止方式调用它,则这是她/他的选择。
Before the introduction of TAP (Task Asynchronous Patter) , there was APM (Asynchronous Programming Model) and EAP (Event-based Asynchronous Pattern) - if I recall correctly, WCF used EAP. 在引入TAP(任务异步模式)之前 ,有APM(异步编程模型)和EAP(基于事件的异步模式) -如果我没记错的话,WCF使用EAP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.