Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Azure Service Bus transport native integration sample

NuGet Package: NServiceBus.Transport.AzureServiceBus (2.x)
Target Version: NServiceBus 7.x

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.

Prerequisites

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

Code walk-through

The sample contains two executable projects:

  • NativeSender - sends native messages to the Receiver's queue.
  • Receiver - NServiceBus endpoint that processes messages sent by NativeSender.

Sending messages with the 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 QueueClient to send a single Message.

Message serialization

The NServiceBus endpoint is using JSON serialization. Therefore, the message sent by a native sender must be valid JSON.

var nativeMessage = new NativeMessage
{
    Content = "Hello from native sender",
    SentOnUtc = DateTime.UtcNow
};

var json = JsonConvert.SerializeObject(nativeMessage);

To generate a serialized message, the MessageGenerator project can be used with the unit test named Generate under the SerializedMessageGenerator test fixture.

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 Message user property.

["NServiceBus.EnclosedMessageTypes"] = typeof(NativeMessage).FullName

Message definition

The message itself is defined using conventions in the Receiver project.

public class NativeMessage
{
    public string Content { get; set; }
    public DateTime SentOnUtc { get; set; }
}

Handling messages from a native sender in an NServiceBus endpoint

Once the message is received by the NServiceBus endpoint, its content 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_ConnectionString environment variable mentioned above.
  • Execute Receiver first to create the destination queue that NativeSender will need to send native messages.

Related Articles