Share via


MSMQ to Windows Communication Foundation

Download sample

This sample demonstrates how a Message Queuing (MSMQ) application can send an MSMQ message to a Windows Communication Foundation (WCF) service. The service is a self-hosted console application to enable you to observe the service receiving queued messages.

The service contract is IOrderProcessor, which defines a one-way service that is suitable for use with queues. An MSMQ message does not have an Action header, so it is not possible to map different MSMQ messages to operation contracts automatically. Therefore, there can be only one operation contract. If you want to define more than one operation contract for the service, the application must provide information as to which header in the MSMQ message (for example, the label or correlationID) can be used to decide which operation contract to dispatch. This is demonstrated in the Custom Demux.

The MSMQ message does not contain information as to which headers are mapped to the different parameters of the operation contract. The parameter is of type MsmqMessage(MsmqMessage<T>), which contains the underlying MSMQ message. The type "T" in the MsmqMessage(MsmqMessage<T>) class represents the data that is serialized into the MSMQ message body. In this sample, the PurchaseOrder type is serialized into the MSMQ message body.

The following sample code shows the service contract of the calculator service.

// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
[ServiceKnownType(typeof(PurchaseOrder))]
public interface IOrderProcessor
{
    [OperationContract(IsOneWay = true, Action = "*")]
    void SubmitPurchaseOrder(MsmqMessage<PurchaseOrder> msg);
}

The service is self-hosted. When using MSMQ, the queue used must be created in advance. This can be done manually or through code. In this sample, the service checks for the existence of the queue and creates it if required. The queue name is read from the configuration file.

public static void Main()
{
    // Get the MSMQ queue name from the application settings in 
    // configuration.
    string queueName = ConfigurationManager.AppSettings["queueName"];
    // Create the MSMQ queue if necessary.
    if (!MessageQueue.Exists(queueName))
        MessageQueue.Create(queueName, true);
    …
}

The service creates and opens a ServiceHost for the OrderProcessorService, as shown in the following sample code.

using (ServiceHost serviceHost = new ServiceHost(typeof(OrderProcessorService)))
{
    serviceHost.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.ReadLine();
    serviceHost.Close();
}

The MSMQ queue name is specified in an appSettings section of the configuration file, as shown in the following sample configuration.

NoteNote:

The queue name uses a dot (.) for the local machine and backslash separators in its path. The WCF endpoint address specifies a msmq.formatname scheme, and uses localhost for the local machine. What follows the msmq.formatname scheme is the address of the queue for each MSMQ Format Name addressing guidelines.

<appSettings>
    <add key="orderQueueName" value=".\private$\Orders" />
</appSettings>

The client application is an MSMQ application that uses the Send method to send a durable and transactional message to the queue, as shown in the following sample code.

//Connect to the queue.
MessageQueue orderQueue = new MessageQueue(ConfigurationManager.AppSettings["orderQueueName"]);

// Create the purchase order.
PurchaseOrder po = new PurchaseOrder();
po.CustomerId = "somecustomer.com";
po.PONumber = Guid.NewGuid().ToString();

PurchaseOrderLineItem lineItem1 = new PurchaseOrderLineItem();
lineItem1.ProductId = "Blue Widget";
lineItem1.Quantity = 54;
lineItem1.UnitCost = 29.99F;

PurchaseOrderLineItem lineItem2 = new PurchaseOrderLineItem();
lineItem2.ProductId = "Red Widget";
lineItem2.Quantity = 890;
lineItem2.UnitCost = 45.89F;

po.orderLineItems = new PurchaseOrderLineItem[2];
po.orderLineItems[0] = lineItem1;
po.orderLineItems[1] = lineItem2;

// Submit the purchase order.
Message msg = new Message();
msg.Body = po;
//Create a transaction scope.
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
    
    orderQueue.Send(msg, MessageQueueTransactionType.Automatic);
    // Complete the transaction.
    scope.Complete();
   
}
Console.WriteLine("Placed the order:{0}", po);
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();

When you run the sample, the client and service activities are displayed in both the service and client console windows. You can see the service receive messages from the client. Press ENTER in each console window to shut down the service and client. Note that because queuing is in use, the client and service do not have to be up and running at the same time. For example, you could run the client, shut it down, and then start up the service and it would still receive its messages.

To setup, build, and run the sample

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

To run the sample across machines

  1. Copy the service program files from the \service\bin\ folder, under the language-specific folder, to the service machine.

  2. Copy the client program files from the \client\bin\ folder, under the language-specific folder, to the client machine.

  3. In the Client.exe.config file, change the orderQueueName to specify the service machine name instead of ".".

  4. On the service machine, launch Service.exe from a command prompt.

  5. On the client machine, launch Client.exe from a command prompt.

See Also

Other Resources

Queues in Windows Communication Foundation
How To: Exchange Messages with WCF Endpoints and MSMQ applications
Message Queuing

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.