WCF Windows服务 - 长操作/回调到调用模块 [英] WCF Windows Service - Long operations/Callback to calling module

查看:164
本文介绍了WCF Windows服务 - 长操作/回调到调用模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,它需要一堆文件的名称,并对它们进行操作(zip /解压缩,更新db等)。操作可能需要时间,具体取决于发送到服务的文件的大小和数量。



(1)向此服务发送请求的模块等待,直到文件处理。我想知道是否有一种方法来提供在服务中的回调,将通知调用模块,当它完成处理文件。请注意,多个模块可以一次调用服务来处理文件,所以服务将需要提供某种类型的TaskId我猜。



(2)如果一个服务方法被调用并且正在运行,并且对同一服务进行另一个调用,那么该调用将如何处理(我认为只有一个线程与服务相关联)。我已经看到,当服务在处理一个方法时花费时间,与服务相关联的线程开始增加。

解决方案

WCF确实提供双工绑定,允许你指定一个回调契约,这样服务可以回调给客户端通知。



然而,在我看来,这个机制是相当片状,不真的要推荐。



在这种情况下,当调用导致相当长的运行操作发生时,我会这样做:



如果你想坚持HTTP / NetTcp绑定,我会:




  • 删除服务的请求,然后let go - 这将是一个单向呼叫,你只是放弃你想做的,然后你的客户端完成

  • 有一个状态调用,客户端可以在给定时间后调用以查明请求的结果是否已准备好

  • 如果是,则应该有第三个服务调用来检索结果



所以在你的情况下,你可以放弃请求压缩一些文件。服务将关闭并执行其工作,并将结果ZIP存储在临时位置。然后稍后客户端可以检查ZIP是否已准备就绪,如果是,则检索。



这在消息队列(MSMQ)在每个Windows服务器机器上(但不是很多人似乎知道或使用它):




  • 您的客户端请求在请求队列上

  • 服务侦听该请求队列并在请求后获取请求,并执行

  • ,然后服务可以发布结果


  • 查看如何通过阅读优秀的MSDN来有效地完成所有这些操作文章服务:建立队列WCF响应服务 - 强烈推荐! / p>

    在我看来,基于消息队列的系统比基于双工/回调协议的系统更稳定,更不容易出错。


    I have a Windows Service that takes the name of a bunch of files and do operations on them (zip/unzip, updating db etc). The operations can take time depending on size and number of files sent to the service.

    (1) The module that is sending a request to this service waits until the files are processed. I want to know if there is a way to provide a callback in the service that will notify the calling module when it is finished processing the files. Please note that multiple modules can call the service at a time to process files so the service will need to provide some kind of a TaskId I guess.

    (2) If a service method is called and is running and another call is made to the same service, then how will that call be processed(I think there is only one thread asociated with the service). I have seen that when the service is taking time in processing a method, the threads associated with the service begin to increase.

    解决方案

    WCF does indeed offer duplex bindings which allow you to specify a callback contract, so that the service can call back to the calling client to notify.

    However, in my opinion, this mechanism is rather flaky and not really to be recommended.

    In such a case, when the call causes a fairly long running operation to happen, I would do something like this:

    If you want to stick to HTTP/NetTcp bindings, I would:

    • drop off the request with the service, and then "let go" - this would be a one-way call, you just drop off what you want to have done, and then your client is done
    • have a status call that the client could call after a given time to find out whether or not the results of the request are ready by now
    • if they are, there should be a third service call to retrieve the results

    So in your case, you could drop off the request to zip some files. The service would go off and do its work and store the resulting ZIP in a temporary location. Then later on the client could check to see whether the ZIP is ready, and if so, retrieve it.

    This works even better over a message queue (MSMQ) which is present in every Windows server machine (but not a lot of people seem to know about it or use it):

    • your client drops off the request on a request queue
    • the service listens on that request queue and fetches request after request and does it works
    • the service can then post the results to a result queue, on which your callers in turn are listening

    Check out how to do all of this efficiently by reading the excellent MSDN article Foudnations: Build a queue WCF Response Service - highly recommended!

    A message-queue based systems tends to be much more stable and less error-prone that a duplex-/callback-contract based system, in my opinion.

    这篇关于WCF Windows服务 - 长操作/回调到调用模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆