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

发布于 2024-10-16 02:42:48 字数 184 浏览 5 评论 0原文

我将在 C# 中使用 MSMQ 来读取消息;我将其放入 Window Service 中,以便在 OnStart 时我将开始使用 queue.Receive 方法读取消息,该方法将是阻塞/同步调用。和 OnEnd 方法我想用queue.Close();停止队列队列.Dispose()。

这种方法有什么缺点吗?

谢谢 海洋

I will be using MSMQ in C# to read messages; 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. And OnEnd method I want to stop the queue with queue.Close(); queue.Dispose().

Is there any drawback of this approach ?

Thanks
Ocean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

清风不识月 2024-10-23 02:42:48

这是不正确的做法。 OnStart 在服务启动时调用一次,您应该放置初始化逻辑。例如,启动将循环调用 Receive 的线程。

This is incorrect approach. OnStart is called once when service starts, you should put initialization logic. For example start thread that will call Receive in a loop.

怪我入戏太深 2024-10-23 02:42:48

这是一种相当常见的模式,但它有一些缺点。

首先,您应该考虑使用线程池(或 4.0 中的 .NET 并行库)来异步处理消息。您的队列读取器是否可以异步很大程度上取决于您的事务模式。处理是原子的吗?

其次,您还应该考虑使用一个计时器 (System.Timers.Timer),该计时器在 OnStart 中启动并在 OnEnd 中结束,并在每个计时器事件上从队列中读取一条或多条消息。

第三,您应该认真考虑仅使用 WCF MSMQ 绑定,它可以处理很多复杂的事情。

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

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. 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.

Third, you should seriously consider just using the WCF MSMQ binding, which handles a lot of the complexity of this stuff.

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

空心↖ 2024-10-23 02:42:48

你的方法对我来说看起来不错。我唯一的建议是确保您打算部署 Windows 服务的每台计算机都位于同一个域中,或者位于相互信任的域中并且驻留在同一个林中。我最近遇到了一个问题,我继承了一个利用 MSMQ 的解决方案,其工作方式与您上面提出的方式大致相同。经过测试,它在单个域上工作,没有性能问题。不幸的是,客户正在进行合并,当需要在更广泛的公司中实施该解决方案时,结果发现该解决方案必须在存在于不同森林的不同域中的计算机上实施,在这种情况下是 MSMQ根本不起作用,必须使用另一种方法。

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. 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. 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.

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