PsychoDUCK
PsychoDUCK

Reputation: 1103

MSMQ Trigger to Call a Windows Service

When ever there is a message in the queue, I need my windows service to process the message in the queue.

I was thinking that it would probably be best to set up a windows service and when ever there is a message in the queue, that MSMQ should trigger, firing a call to the windows service.

Does anyone know how to do this?

Upvotes: 2

Views: 4361

Answers (2)

Dominic Zukiewicz
Dominic Zukiewicz

Reputation: 8444

If you wanted to, you can host a WCF ServiceHost in the Windows Service, which will automatically pick up the messages as they are received. No hook-ups to MSMQ are required. WCF will automatically pull the messages into the service when they appear.

Lets say you are already writing to the MSMQ private queue 'test'. To write a running Windows Service, you do something like this, forgive the example for method accuracy in the service:

namespace WcfService
{
  public class Order 
  { 
    public int ID { get; set; }
  }

  [ServiceContract]
  public interface IOrderService
  {
    [OperationContract(TransactionScopeRequired=true)]
    void ProcessOrder(Order order);
  }

  public class OrderService : IOrderService
  {
    public void ProcessOrder(Order order)
    {
      Debug.Print("Order ID: ", order.ID);
    }
  }
}

namespace Client
{
  public class WindowsService : IDisposable
  {
    private ServiceHost host = null;

    // TODO: Implement static void Main to initialize service

    // OnStart
    public void OnStart()
    {
      if(host == null)
        host = new ServiceHost( typeof ( OrderService ) );

      host.Open();
    }   

    public void OnStop()
    {
      host.Close();
    }

    public void Dispose()
    {
      // TODO: Implement Dispose() pattern properly
      if(host!=null) 
        host.Dispose();
    }
  }
}

Then configuration for the Windows Service to read from MSMQ

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netMsmqBinding>
                <binding name="readFromQueueBinding" exactlyOnce="false">
                    <security mode="None"/>
                </binding>
            </netMsmqBinding>
        </bindings>
        <services>
            <service name="WcfService.OrderService">
                <endpoint address="net.msmq://localhost/private/test" 
                          binding="netMsmqBinding" 
                          contract="WcfService.IOrderService" 
                          name="IOrderService_Msmq_Service" 
                          bindingConfiguration="readFromQueueBinding"/>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Upvotes: 2

kprobst
kprobst

Reputation: 16651

This is an overly broad question, but you can create a Windows service that listens to a queue syncronously or asynchronously, receives messages and processes them as they arrive. As to how to accomplish that, the default service project provided by Visual Studio is a good start. From there you can create a class that gets instantiated when the service starts, binds to the queue and calls Receive or BeginReceive (sync or async) to get messages and process them.

Another option is to use activation services. This is potentially more complicated from an environmental perspective, and you need a certain version of Windows and .NET for it to be available.

Finally there's MSMQ Triggers. This is a service that is (optionally) installed along with MSMQ itself. It can be configured to monitor a queue and perform an action when a message arrives. This would be the simplest option, however if you opt for this then I'd suggest just creating a normal EXE instead of a service, and using the trigger to execute it.

This article covers some of the pros and cons of each approach; I'd suggest reading it before you make a decision.

Upvotes: 2

Related Questions