This documentation is archived and is not being maintained.

NetMsmqBinding Class

Represents a queued binding that is suitable for cross-machine communication.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public ref class NetMsmqBinding : public MsmqBindingBase

The NetMsmqBinding binding provides support for queuing by leveraging Microsoft Message Queuing (MSMQ) as a transport and enables support for loosely coupled applications, failure isolation, load leveling and disconnected operations. For a discussion of these features, see Queues.

This is one of the standard bindings provided by Windows Communication Foundation (WCF). The recommended procedure is to define the binding using configuration values and not to use a code-based approach, except in certain advanced scenarios where configuration values must be set as a service is initialized.

The following example shows how to configure a service to use the NetMsmqBinding binding:

First, the config file:

<!-- This is the service config file -->

    <!-- use appSetting to configure MSMQ queue name -->
    <add key="queueName" value=".\private$\ServiceModelSamples" />
    <add key ="baseAddress" value="http://localhost:8000/queuedCalculator/defaultSample"/>

        <!-- Define NetMsmqEndpoint -->
        <endpoint address="net.msmq://localhost/private/ServiceModelSamples"
                  contract="Microsoft.ServiceModel.Samples.IQueueCalculator" />
        <endpoint address="mex"
                  contract="IMetadataExchange" />

        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>

Next, the actual service code:

// Define a service contract. 
public interface IQueueCalculator
    void Add(double n1, double n2);
    [OperationContract(IsOneWay = true)]
    void Subtract(double n1, double n2);
    [OperationContract(IsOneWay = true)]
    void Multiply(double n1, double n2);
    [OperationContract(IsOneWay = true)]
    void Divide(double n1, double n2);
// Service class that implements the service contract. 
// Added code to write output to the console window 
public class CalculatorService : IQueueCalculator
    public void Add(double n1, double n2)
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1}) - result: {2}", n1, n2, result);

    public void Subtract(double n1, double n2)
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1}) - result: {2}", n1, n2, result);

    public void Multiply(double n1, double n2)
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1}) - result: {2}", n1, n2, result);

    public void Divide(double n1, double n2)
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1}) - result: {2}", n1, n2, result);
// This is the hosting application. This code can appear directly in the service class as well. 
class HostApp
    // Host the service within this EXE console application. 
    public static void Main()
        // Get MSMQ queue name from appsettings in configuration.
        string queueName = ConfigurationManager.AppSettings["queueName"];

        // Create the transacted MSMQ queue if necessary. 
        if (!MessageQueue.Exists(queueName))
            MessageQueue.Create(queueName, true);

        // Get the base address that is used to listen for WS-MetaDataExchange requests. 
        // This is useful to generate a proxy for the client.
        string baseAddress = ConfigurationManager.AppSettings["baseAddress"];

        // Create a ServiceHost for the CalculatorService type. 
        using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), new Uri(baseAddress)))
            // Open the ServiceHostBase to create listeners and start listening for messages.

            // The service can now be accessed.
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");

            // Close the ServiceHostBase to shutdown the service.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0