Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring

Gateway

Component: Gateway
NuGet Package: NServiceBus.Gateway (5.x)
Target Version: NServiceBus 9.x

Code walk-through

This sample demonstrates how logically different sites (such as a headquarters and a remote site) can communicate using the NServiceBus Gateway feature.

Headquarters.Shared

A shared class library for common code including message definitions.

Headquarters

The central endpoint of the Sample.

Gateway configuration

  • Maps the site key RemoteSite to http://localhost:25899/RemoteSite
  • Receives incoming messages on http://localhost:25899/Headquarters/
var gatewayConfig = endpointConfiguration.Gateway(new NonDurableDeduplicationConfiguration());
gatewayConfig.AddReceiveChannel("http://localhost:25899/Headquarters/");
gatewayConfig.AddSite("RemoteSite", "http://localhost:25899/RemoteSite/");

AcknowledgedHandler

Handles PriceUpdateAcknowledged from RemoteSite.

public class AcknowledgedHandler : IHandleMessages<PriceUpdateAcknowledged>
{
    static readonly ILog log = LogManager.GetLogger<AcknowledgedHandler>();

    public Task Handle(PriceUpdateAcknowledged message, IMessageHandlerContext context)
    {
        log.Info($"Price update received by: {message.BranchOffice}");
        return Task.CompletedTask;
    }
}

UpdatePriceHandler

Handles UpdatePrice from WebClient.

public class UpdatePriceHandler : IHandleMessages<UpdatePrice>
{
    static readonly ILog log = LogManager.GetLogger<UpdatePriceHandler>();

    public Task Handle(UpdatePrice message, IMessageHandlerContext context)
    {
        log.Info("Price update received from the webclient, going to push to RemoteSite");
        string[] siteKeys =
        [
            "RemoteSite"
        ];
        var priceUpdated = new PriceUpdated
        {
            ProductId = message.ProductId,
            NewPrice = message.NewPrice,
            ValidFrom = message.ValidFrom,
        };
        return context.SendToSites(siteKeys, priceUpdated);
    }
}

RemoteSite

Gateway configuration

Receives incoming messages on http://localhost:25899/RemoteSite/

var gatewayConfig = endpointConfiguration.Gateway(new NonDurableDeduplicationConfiguration());
gatewayConfig.AddReceiveChannel("http://localhost:25899/RemoteSite/");

PriceUpdatedHandler

public class PriceUpdatedHandler : IHandleMessages<PriceUpdated>
{
    static readonly ILog log = LogManager.GetLogger<PriceUpdatedHandler>();

    public Task Handle(PriceUpdated message, IMessageHandlerContext context)
    {
        var header = context.MessageHeaders[Headers.OriginatingSite];
        log.Info($"Price update for: {message.ProductId} received. Reply over channel: {header}");

        var updateAcknowledged = new PriceUpdateAcknowledged
        {
            BranchOffice = "RemoteSite"
        };
        return context.Reply(updateAcknowledged);
    }
}

WebClient

This project represents an external integration point. It sends a UpdatePrice to the channel http://localhost:25899/Headquarters/.

var message = "<?xml version=\"1.0\" ?><Messages xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><UpdatePrice><ProductId>23</ProductId></UpdatePrice></Messages>";
var md5 = b64_md5(message) + "==";
var clientId = Math.uuid() + "\\123456";

$.ajax({
    url: $('#gatewayaddress').val(),
    data: {
        Message: message,
        "NServiceBus.CallType": "SingleCallSubmit",
        "NServiceBus.AutoAck": "true",
        "Content-MD5": md5,
        "NServiceBus.Id": clientId,
        "NServiceBus.EnclosedMessageTypes": "Shared.UpdatePrice, Shared"
    },
    dataType: 'jsonp',
    success: function (data) {
        if (data.status != "OK") {
            alert("Failed to submit the request to the gateway");
            return;
        }
        alert("Success - Check the output of the headquarters server process");
    },
    error: function (http, status) {
        alert("Failed submit: " + status);
    }
});

Related Articles

  • Gateway
    Durable fire-and-forget messaging across physically separated IT infrastructure.