Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Bridge messages between endpoints using MSMQ and Azure Service Bus

Component: Messaging Bridge
NuGet Package: NServiceBus.MessagingBridge (3.x)
Target Version: NServiceBus 9.x

Endpoints running on different transports can not exchange messages directly and must use the NServiceBus.MessagingBridge component to communicate.

Common examples include:

  • A hybrid solution that spans across endpoints deployed on-premises and in a cloud environment.
  • Departments within organization integrating their systems that use different messaging technologies for historical reasons.

Traditionally, these integrations would require native messaging or relaying. Bridging is an alternative, allowing endpoints to communicate over different transports without getting into the low-level messaging technology.

Prerequisites

An environment variable named AzureServiceBus_ConnectionString with the connection string for the Azure Service Bus namespace.

Code walk-through

This sample shows an integration between two endpoints running on two different transports: MsmqEndpoint running on MSMQ and AsbEndpoint running on Azure Service Bus.

The scenarios covered by the sample include:

  • Sending commands from an Azure Service Bus endpoint to an MSMQ endpoint.
  • Publishing events from an MSMQ endpoint and subscribing to those events from an Azure Service Bus endpoint.
  • Publishing events from Azure Service Bus and subscribing to those events from an MSMQ endpoint.

msmq to azure service bus messaging bridge sample

Bridging

Endpoints are bridged using NServiceBus.MessagingBridge, which is a standalone process that runs side-by-side with the bridged endpoints, MsmqEndpoint and AsbEndpoint. These endpoints are not aware that there is a bridge involved in the sending and receiving of messages. They send/publish messages as if entire system uses the same transport. All of the configuration to bridge different transports is handled in the bridge code.

The bridge is connecting the MSMQ and Azure Service Bus endpoints and providing the configuration settings required by each transport. For example, Azure Service Bus requires a connection string and the topology to be set.

Azure Service Bus bridge endpoint configuration

The Azure Service Bus bridge endpoint is configured by using the name of the actual Azure Service Bus endpoint where the message needs to be routed to:

var asbBridgeEndpoint = new BridgeEndpoint("Samples.MessagingBridge.AsbEndpoint");

To subscribe to an event published by the MSMQ endpoint, the Azure Service Bus endpoint must register the publishing endpoint:

asbBridgeEndpoint.RegisterPublisher<MyEvent>("Samples.MessagingBridge.MsmqEndpoint");

When the bridge endpoint has been created and the publisher has been registered, the endpoint is added to the transport and then the transport is added to the bridge configuration.

var asbBridgeTransport = new BridgeTransport(new AzureServiceBusTransport(connectionString))
{
    AutoCreateQueues = true
};

asbBridgeTransport.HasEndpoint(asbBridgeEndpoint);
bridgeConfiguration.AddTransport(asbBridgeTransport);

MSMQ bridge endpoint configuration

The MSMQ bridge endpoint is configured by using the name of the actual MSMQ endpoint where the message needs to be routed to:

var msmqBridgeEndpoint = new BridgeEndpoint("Samples.MessagingBridge.MsmqEndpoint");

To subscribe to an event published by the Azure Service Bus endpoint, the MSMQ endpoint must register the publishing endpoint:

msmqBridgeEndpoint.RegisterPublisher<OtherEvent>("Samples.MessagingBridge.AsbEndpoint");

When the bridge endpoint has been created and the publisher has been registered, the endpoint is added to the transport and then the transport is added to the bridge configuration.

var msmqBridgeTransport = new BridgeTransport(new MsmqTransport())
{
    AutoCreateQueues = true
};

msmqBridgeTransport.HasEndpoint(msmqBridgeEndpoint);
bridgeConfiguration.AddTransport(msmqBridgeTransport);

Related Articles