Prerequisites
An environment variable named AzureServiceBus.
with the connection string for the Azure Service Bus namespace.
Azure Service Bus Transport
This sample uses the Azure Service Bus Transport (Legacy).
Code walk-through
This sample shows how to send a message from non-NServiceBus code using the Azure Service Bus API and process it with an NServiceBus endpoint using the Azure Service Bus transport.
The sample contains two executable projects:
NativeSender
- sends a nativeBrokeredMessage
messages to a queue.Receiver
- NServiceBus endpoint that processes messages sent byNativeSender
.
Sending messages with native Azure Service Bus API
Configuring the native sender to send messages to the queue used by the receiving endpoint is required when integrating the Azure Service Bus sender with NServiceBus endpoints. By default, the input queue for an NServiceBus endpoint is its endpoint name.
var endpointConfiguration = new EndpointConfiguration("Samples.ASB.NativeIntegration");
The native sender is using queue client to send a BrokeredMessage
.
Message serialization
The Azure Service Bus transport is using the JSON serializer by default. Therefore, the message sent by a native sender needs to be valid JSON.
var nativeMessage = @"{""Content"":""Hello from native sender"",""SentOnUtc"":""2015-10-27T20:47:27.4682716Z""}";
To generate a serialized message, the MessageGenerator
project can be used with the unit test named Generate
under the SerializedMessageGenerator
test fixture.
BrokeredMessage body format
The Azure Service Bus API allows the construction of a BrokeredMessage
body from a stream or an object that will get serialized by the internals of BrokeredMessage
.
Message type detection
The native message must allow NServiceBus to detect the message type either via the headers or the message payload.
In this sample, the header option is used by storing the FullName
of the message as an Azure Service Bus BrokeredMessage
user property.
message.Properties["NServiceBus.EnclosedMessageTypes"] = "NativeMessage";
message.Properties["NServiceBus.Transport.Encoding"] = "application/octect-stream";
// Required to support ServiceControl that is using ASB v6.x
message.Properties["NServiceBus.MessageIntent"] = "Send";
Message definition
The message itself is defined as an IMessage
in the Shared
project.
public class NativeMessage :
IMessage
{
public string Content { get; set; }
public DateTime SentOnUtc { get; set; }
}
NServiceBus receiving endpoint
The receiver is defining how to get the Azure Service Bus transport message body by specifying a strategy using BrokeredMessageBodyConversion
.
var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
var topologySettings = transport.UseEndpointOrientedTopology();
topologySettings.BrokeredMessageBodyType(SupportedBrokeredMessageBodyTypes.Stream);
Handling messages from native sender in an NServiceBus endpoint
Once the message is received by the NServiceBus endpoint, its contents will be presented.
public class NativeMessageHandler :
IHandleMessages<NativeMessage>
{
static ILog log = LogManager.GetLogger<NativeMessageHandler>();
public Task Handle(NativeMessage message, IMessageHandlerContext context)
{
log.Info($"Message content: {message.Content}");
log.Info($"Received native message sent on {message.SentOnUtc} UTC");
return Task.CompletedTask;
}
}
Things to note
- The use of the
AzureServiceBus.
environment variable mentioned above.ConnectionString - The use of
UseSingleBrokerQueue
prevents the Azure Service Bus transport individualizing queue names by appending the machine name. - Execute
Receiver
first to create destination queueNativeSender
will need to send native messages.