newbie
newbie

Reputation:

Design pattern for windows service - c#

I am getting my feet wet with programming and my self learning brings me to this question. I have been reading about design patterns and wanted to know what a good recommendation for this would be.

My test application sends a message to MSMQ. I want to write a windows service which will listen to this MSMQ and for every message it receives, I want it to perform a simple database insert.

I looked at this articlelink text

which gave me some information i needed. In the example I could do most of my work within

 private static void MyReceiveCompleted(Object source, 
        ReceiveCompletedEventArgs asyncResult)
    {
        // Connect to the queue.
        MessageQueue mq = (MessageQueue)source;

        // End the asynchronous Receive operation.
        Message m = mq.EndReceive(asyncResult.AsyncResult);

        // Display message information on the screen.
        Console.WriteLine("Message: " + (string)m.Body);

        // Restart the asynchronous Receive operation.
        mq.BeginReceive();

        return; 
    }

But is there a specific pattern that is more viable candidate? From reading around I assume it could be the 'command' pattern but I am not sure if this is the best pattern. Any ideas or thoughts to help me get a better understanding of the patterns.

Books I read on patterns: Head First Design patterns - i know mixed reviews on this ordered a used copy of design patterns by gang of four(sp?)

Upvotes: 3

Views: 3773

Answers (3)

Jason Coyne
Jason Coyne

Reputation: 6636

This is not directly related to your question, but a key design pattern to developing services of any kind (web services, windows services, etc) is to seperate your functionality from the service host.

That is, have a class that actually performs the work. And have a service that calls the class.

This way you can re-host the service easily in multiple hosts, and have an easy way to test the functionality via console apps, winforms apps, or unit tests.

Upvotes: 5

Andy White
Andy White

Reputation: 88475

If you can use WCF in your system, check out the netMsmqBinding or msmqIntegrationBinding. It's actually not too hard to setup, there are many tutorials out there.

With WCF, you won't have to deal with any System.Messaging stuff.

At my company, we host a lot of MSMQ services in windows services. Look at the ServiceHost class in WCF.

Upvotes: 1

JoshBerke
JoshBerke

Reputation: 67108

When processing messages from a queue I like using a Message Dispatcher.

Upvotes: 0

Related Questions