如何取消WCF服务调用?

发布于 2024-07-22 18:02:56 字数 129 浏览 7 评论 0原文

我有一个执行时间很长的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平生欢 2024-07-29 18:02:56

您可能不需要后台工作人员。 您可以使用 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.

爱已欠费 2024-07-29 18:02:56

创建一个 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.

那片花海 2024-07-29 18:02:56

如果您使用异步调用,需要了解的一件重要的事情是,仍然无法取消请求并阻止服务启动后返回响应。 您的 UI 需要智能地处理响应以避免竞争条件。 幸运的是,有一种简单的方法可以做到这一点。

此示例用于搜索订单 - 由 UI 驱动。 假设可能需要几秒钟才能返回结果,并且用户正在连续运行两个搜索。

因此,如果您的用户运行两次搜索,并且第一个搜索在第二个搜索之后返回 - 您需要确保不显示第一个搜索的结果。

   private int _searchRequestID = 0; // need one for each WCF method you call


   // Call our service...
   // The call is made using the overload to the Async method with 'UserToken'.
   // When the call completes we check the ID matches to avoid a nasty
   // race condition
   _searchRequestID = _searchRequestID++;
   client.SearchOrdersCompleted += (s, e) =>
   {
       if (_searchRequestID != (int)e.UserState))
       {
           return; // avoid nasty race condition
       }

       // ok to handle response ...
    }
    client.SearchOrdersAsync(searchMessage, _searchRequestID);

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.

   private int _searchRequestID = 0; // need one for each WCF method you call


   // Call our service...
   // The call is made using the overload to the Async method with 'UserToken'.
   // When the call completes we check the ID matches to avoid a nasty
   // race condition
   _searchRequestID = _searchRequestID++;
   client.SearchOrdersCompleted += (s, e) =>
   {
       if (_searchRequestID != (int)e.UserState))
       {
           return; // avoid nasty race condition
       }

       // ok to handle response ...
    }
    client.SearchOrdersAsync(searchMessage, _searchRequestID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文