[OperationContract(IsOneWay = true)] 和选中生成的异步操作复选框有什么区别?
有什么区别
使用[OperationContract(IsOneWay = true)]
属性标记 WCF 方法与添加服务引用时选中生成异步操作复选框
?据我所知,调用的异步性质似乎只能在客户端定义。 如果是这样的话,[OperationContract(IsOneWay = true)]
的意义何在?
现在,我只在 WCF 方法中运行以下方法。
public void UpdateIndex(IndexElement[] indexElements)
{
// start the update on a new thread.
Thread thread = new Thread(() => UpdateIndexThread(indexElements));
thread.Start();
}
我在客户端代码中创建了一个服务引用,然后只需调用:
indexerClient.UpdateIndex(indexElements);
其中 indexerClient
是我的 WCF 服务的实例。
这也应该有效吗?似乎没有,几乎就像它等待线程完成然后返回一样。
What is the difference between marking a WCF method with
[OperationContract(IsOneWay = true)]
attribute and checking the generate asynchronous operations checkbox when adding a service reference?
From what I have read, it seems the asynchronous nature of the call should only be defined on the client side.
If that's the case, what is the point of the [OperationContract(IsOneWay = true)]
?
Right now, I just have the following method running in the WCF method.
public void UpdateIndex(IndexElement[] indexElements)
{
// start the update on a new thread.
Thread thread = new Thread(() => UpdateIndexThread(indexElements));
thread.Start();
}
I created a service reference in my client's code, and I simply call:
indexerClient.UpdateIndex(indexElements);
Where indexerClient
is an instance of my WCF service.
Should this also work? It doesn't seem to, it's almost as though it waits for the thread to complete before returning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些是非常不同的。
在概念层面上,IsOneWay=true 表示消息传递模式是“即发即忘”,而不是“请求-响应”等。即IOW=true表示有客户端到服务器的消息,但服务器没有回复客户端。相反,非 IOW=true 方法通常会有响应消息,即使返回类型为 void(例如“空”消息)。
异步模式是关于客户端代码的行为方式 - 例如,它是否阻塞等待返回值。异步是“本地”事物,请参阅此博客了解详情。您可以为同步服务器设置一个异步客户端,也可以为异步服务器设置一个同步客户端。 WCF 将在幕后发挥神奇作用,为您提供任一编程模型。如果您有一个请求-响应消息传递模式并使用“生成异步”,则生成的客户端将为您提供一个可以调用异步的方法(例如发送消息,并在回复到达时获取回调)。
因此,使用“异步”表示“本地编程模型”,使用 IOW 表示“在线消息传递”。
请注意,在您的示例中,如果您标记方法 IOW=true,那么我认为服务器代码中没有 Thread.Start() 的原因。您可以直接在 WCF 为您的服务器提供的线程上完成工作。
These are very different.
At a conceptual level, IsOneWay=true says that the messaging pattern is 'fire and forget' as opposed to e.g. 'request-response'. That is, IOW=true means there is a message from the client to the server, but not a reply from the server to the client. In contrast, a non-IOW=true method will typically have a response message, even if the return type is void (e.g. an 'empty' message).
The async pattern is for how the client code behaves - e.g. does it block waiting for the return value or not. Async is a 'local' thing, see this blog for details. You can have an async client for a sync server or a sync client for an async server. WCF will do the magic under the hood to give you either programming model. If you have e.g. a request-response messaging pattern and use 'generate async', the generated client will give you e.g. a method you can call async (e.g. send the message, and get a callback when the reply arrives).
So use 'async' for 'local programming model', and use IOW for 'messaging on the wire'.
Note that in your example, if you mark the method IOW=true, then I think there is no reason for the Thread.Start() in the server code. You can just do the work right there on the thread WCF has given your server.