Endpoints running on different transports can not exchange messages directly and must use the NServiceBus.
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.
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));
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.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());
msmqBridgeTransport.AutoCreateQueues = true;
msmqBridgeTransport.HasEndpoint(msmqBridgeEndpoint);
bridgeConfiguration.AddTransport(msmqBridgeTransport);