Export (0) Print
Expand All

NetMsmqBinding Class

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

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

'Declaration
Public Class NetMsmqBinding _
	Inherits MsmqBindingBase
'Usage
Dim instance As NetMsmqBinding

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 -->
<configuration>

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

  <system.serviceModel>
    <services>
      <service 
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <!-- Define NetMsmqEndpoint -->
        <endpoint address="net.msmq://localhost/private/ServiceModelSamples"
                  binding="netMsmqBinding"
                  contract="Microsoft.ServiceModel.Samples.IQueueCalculator" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  </configuration>

Next, the actual service code:

// Define a service contract. 
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface IQueueCalculator
{
    [OperationContract(IsOneWay=true)]
    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
{
    [OperationBehavior]
    public void Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1}) - result: {2}", n1, n2, result);
    }

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

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

    [OperationBehavior]
    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.
            serviceHost.Open();

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

            // Close the ServiceHostBase to shutdown the service.
            serviceHost.Close();
        }
    }
}

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

Community Additions

ADD
Show:
© 2014 Microsoft