Native Integration Azure Service Bus .NET Standard Transport Sample

Component: Azure Service Bus Transport for .NET Standard
NuGet Package NServiceBus.Transport.AzureServiceBus (1-pre)
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Prerequisites

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

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 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",
    SendOnUtc = 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.

Required headers

For a native message to be processed, NServiceBus endpoints using the Azure Service Bus .NET Standard transport need the message type to be included as a header. This header must be stored as an Azure Service Bus Message user property.

["NServiceBus.EnclosedMessageTypes"] = "NativeMessage",
The NServiceBus.EnclosedMessageTypes property must contain the the fully-qualified name of the type expected by the NServiceBus endpoint.

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

To specify a message ID different from the underlying transport message ID (Message.MessageId), set the NServiceBus.MessageId header on the native message with the desired message ID.
public class NativeMessage
{
    public string Content { get; set; }
    public DateTime SendOnUtc { 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.SendOnUtc} 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


Last modified