0

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

Ocean
  • 655
  • 2
  • 8
  • 21

3 Answers3

2

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.

Andrey
  • 59,039
  • 12
  • 119
  • 163
1

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.

Rachel
  • 21
  • 1
  • I assume your queues were public rather than private. Private queues don't care about domains/Active Directory with the exception of verifying permissions to access those queues. – RMD Feb 04 '11 at 20:54
  • "with the exception of verifying permissions to access those queues" - that's the key part of that statement. – Rachel Feb 04 '11 at 21:14
  • Authenticating MSMQ messages between forests http://blogs.msdn.com/b/johnbreakwell/archive/2008/10/13/authenticating-msmq-messages-between-forests.aspx Cross-forest MSMQ? You need to be trusting http://blogs.msdn.com/b/johnbreakwell/archive/2008/06/27/cross-forest-msmq-you-need-to-be-trusting.aspx "How do I send MSMQ messages between domains?" http://blogs.msdn.com/b/johnbreakwell/archive/2008/02/14/how-do-i-send-msmq-messages-between-domains.aspx – John Breakwell Feb 05 '11 at 00:15
1

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

RMD
  • 3,421
  • 7
  • 39
  • 85