Bridge messages between endpoints using MSMQ and Azure Service Bus

Component: Transport Bridge
NuGet Package: NServiceBus.Transport.Bridge (1-pre)

This is a Preview project
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

Endpoints running on different transports can not exchange messages directly and must use the NServiceBus.Transport.Bridge 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 transport bridge sample

Bridging

Endpoints are bridged using NServiceBus.Transport.Bridge, 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.Transport.Bridge.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.Transport.Bridge.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));
asbBridgeTransport.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:

The QueueAddress parameter is needed to create an MSMQ bridge endpoint when the actual MSMQ endpoint and the bridge are running on separate servers
var msmqBridgeEndpoint = new BridgeEndpoint("Samples.Transport.Bridge.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.Transport.Bridge.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());
msmqBridgeTransport.AutoCreateQueues = true;
msmqBridgeTransport.HasEndpoint(msmqBridgeEndpoint);
bridgeConfiguration.AddTransport(msmqBridgeTransport);

Related Articles

  • Azure Service Bus Transport
  • Bridge
    Connect endpoints in a system that use different transports with the transport bridge.
  • MSMQ Transport
    MSMQ is a solid durable communications technology but does not dynamically detect network interfaces.

Last modified