简体   繁体   English

使用服务(ServiceBase)的MSMQ C#实现

[英]MSMQ C# implementation using service (ServiceBase)

I will be using MSMQ in C# to read messages; 我将在C#中使用MSMQ读取消息; and I am putting this in Window Service so on OnStart I will start reading messages using queue.Receive method which would be blocking/synchronous call. 并将其放在Window Service中,因此在OnStart上,我将开始使用queue.Receive方法读取消息,该方法将阻塞/同步调用。 And OnEnd method I want to stop the queue with queue.Close(); 和OnEnd方法,我想使用queue.Close()停止队列。 queue.Dispose(). queue.Dispose()。

Is there any drawback of this approach ? 这种方法有什么缺点吗?

Thanks Ocean 谢谢海洋

This is incorrect approach. 这是不正确的方法。 OnStart is called once when service starts, you should put initialization logic. 服务启动后,一旦调用OnStart ,则应放置初始化逻辑。 For example start thread that will call Receive in a loop. 例如,启动线程将循环调用Receive

Your approach looks fine to me. 您的方法对我来说很好。 My only advice is to make sure that every machine on which you intend to deploy the Windows Service is either in the same Domain, or are in Domains with mutual trust and that reside within the same Forest . 我唯一的建议是确保要在其上部署Windows Service的每台计算机位于同一域中,或者位于相互信任的域中并且位于同一林中 I had a problem recently with a solution that I inherited that utilised MSMQ, and that worked much the same way as you have proposed above. 最近,我继承了一个使用MSMQ的解决方案时遇到了问题,该解决方案的工作方式与您上面提出的方法大致相同。 It was tested as working on a single domain with no performance issues. 经过测试,它可以在单个域上工作,而没有性能问题。 Unfortunately, the client was in the process of a merger, and when it came time to implement the solution across the wider company it turned out that the solution had to be implemented on machines that existed in different domains in different Forests, in which case MSMQ wont work at all and another approach entirely had to be used. 不幸的是,客户正在合并中,当需要在更广泛的公司中实施该解决方案时,事实证明该解决方案必须在不同林中不同域中存在的计算机上实施,在这种情况下,MSMQ根本行不通,必须完全使用另一种方法。

This is a fairly common pattern, but it has some drawbacks. 这是一种很常见的模式,但是它有一些缺点。

First, you should consider using a thread pool (or the .NET parallel libs in 4.0) to process your messages asynchronously. 首先,您应该考虑使用线程池(或4.0中的.NET并行库)异步处理消息。 Whether or not your queue reader can be asynchronous depends a lot on your transaction pattern. 队列读取器是否可以异步取决于您的事务模式。 Will the processing be atomic? 处理会是原子的吗?

Second, you should also consider using a timer (System.Timers.Timer) which you start in your OnStart and end in your OnEnd, and which reads one or more messages from the queue on each timer event. 其次,您还应该考虑使用一个计时器(System.Timers.Timer),该计时器从OnStart开始,在OnEnd结束,并且在每个计时器事件上从队列中读取一个或多个消息。

Third, you should seriously consider just using the WCF MSMQ binding, which handles a lot of the complexity of this stuff. 第三,您应该认真考虑仅使用WCF MSMQ绑定,该绑定可以处理很多此类工作。

See: http://jamescbender.com/bendersblog/archive/2009/04/04/the-one-where-i-talk-about-the-msmq-binding.aspx 参见: http : //jamescbender.com/bendersblog/archive/2009/04/04/the-one-where-i-talk-about-the-msmq-binding.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM