How to write a WCF service to grab a message from MSMQ?

52 Views Asked by At

So I'm sending messages to a queue with another service. I currently have a service set up that manually runs, opens a connection to the queue, grabs a message, inserts into DB, then stops running. I want to use WCF so that my service runs continuously and grabs a message from the queue whenever one appears.

I have a DataContract class that details the object that I'm sending to the queue, do I need to write a separate service operation contract and detail a handler method to be called when a message arrives? Or would I put that method in my DataContract class that I've already wrote? Kind of lost on what additional things I need to write to get WCF up and going.

If I have to write a Service Operation Contract class, would it be just as basic as adding a handler method that takes a msmqmessage parameter? And after having that set, what do I need to have in my config file? I already have the queue address which I used to simply grabbing the message manually, but do I need to add additional details to get WCF up and running and pointing to the queue?

Here's how my manual message grabbing currently looks -

   private static void GrabFromQueue()
   {
        MessageQueue MyQueue = new MessageQueue(_queuePath);

        MyQueue.Formatter = new XmlMessageFormatter(new Type[]
            {typeof(MyContract)});

        MessageQueueTransaction myTransaction = new
            MessageQueueTransaction();

        try
        {
            myTransaction.Begin();

            Message myMessage = MyQueue.Receive(myTransaction);

            _myContract = (FolderScanResultContract)myMessage.Body; //setting global variable initialized in the beginning to the object I just grabbed from the queue

            myTransaction.Commit();
        }

        catch (MessageQueueException e)
        {
            myTransaction.Abort();
        }
        return;
    }
0

There are 0 best solutions below