如何取消WCF服务调用?
我有一个执行时间很长的WCF函数,所以我用backgraundworker在UI中调用该函数...我想提供一个取消执行的功能,所以我中止IComunicationObject,问题是服务执行没有停止,是在这种情况下有什么方法可以停止服务执行吗?
I have a WCF function that is executing long time, so I call the function in UI with backgraundworker... I want to give a feature to cancel the execution, so I abort IComunicationObject, the problem is that Service execution is not stoping, Is there any way to stop Service execution in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能不需要后台工作人员。 您可以使用 IsOneWay 操作,或实现异步模式。 为了防止线程问题,请考虑使用 SynchronizationContext。 编程 WCF 服务 在解释这些方面做得很好。
You may not need a BackgroundWorker. You can either make the operation IsOneWay, or implement the asynchronous pattern. To prevent threading issues, consider using the SynchronizationContext. Programming WCF Services does a great job at explaining these.
创建一个 CancelOperation() 方法,在您的服务中设置一些静态 ManualResetEvent。 经常在您的操作方法中检查此事件。 或者,如果您的服务可以同时处理多个操作调用,则可以是 CancelOperation(Guid operationId)。
Make a CancelOperation() method which sets some static ManualResetEvent in your service. Check this event in your Operation method frequently. Or it can be CancelOperation(Guid operationId) if your service can process multiple operation calls concurrently.
如果您使用异步调用,需要了解的一件重要的事情是,仍然无法取消请求并阻止服务启动后返回响应。 您的 UI 需要智能地处理响应以避免竞争条件。 幸运的是,有一种简单的方法可以做到这一点。
此示例用于搜索订单 - 由 UI 驱动。 假设可能需要几秒钟才能返回结果,并且用户正在连续运行两个搜索。
因此,如果您的用户运行两次搜索,并且第一个搜索在第二个搜索之后返回 - 您需要确保不显示第一个搜索的结果。
One important thing to understand if you're using the Async calls is that there's still no way to cancel a request and prevent a response coming back from the service once it's started. It's up to your UI to be intelligent in handling responses to avoid race conditions. Fortunately there's a simple way of doing this.
This example is for searching orders - driven by a UI. Lets assume it may take a few seconds to return results and the user is running two searches back to back.
Therefore if your user runs two searches and the first search returns after the second - you need to make sure you don't display the results of the first search.