Gateway

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

Code walk-through

This sample demonstrates how logically different sites (such as a headquarters and a remote site) 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/
<GatewayConfig>
  <Sites>
    <Site Key="RemoteSite"
          Address="http://localhost:25899/RemoteSite/"
          ChannelType="Http" />
  </Sites>
  <Channels>
    <Channel Address="http://localhost:25899/Headquarters/"
             ChannelType="Http" />
  </Channels>
</GatewayConfig>

AcknowledgedHandler

Handles PriceUpdateAcknowledged from RemoteSite.

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

    public void Handle(PriceUpdateAcknowledged message)
    {
        log.Info($"Price update received by: {message.BranchOffice}");
    }
}

UpdatePriceHandler

Handles UpdatePrice from WebClient.

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

    public UpdatePriceHandler(IBus bus)
    {
        this.bus = bus;
    }

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

RemoteSite

Gateway configuration

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

<GatewayConfig TransactionTimeout="00:10:00">
  <Channels>
    <Channel Address="http://localhost:25899/RemoteSite/"
             ChannelType="Http"
             Default="true" />
  </Channels>
</GatewayConfig>

PriceUpdatedHandler

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

    public PriceUpdatedHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(PriceUpdated message)
    {
        var header = bus.GetMessageHeader(message, Headers.OriginatingSite);
        log.Info($"Price update for product: {message.ProductId} received. Going to reply over channel: {header}");

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

WebClient

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

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.TimeToBeReceived": "00:10:00",
        "NServiceBus.EnclosedMessageTypes": "Shared.UpdatePrice, Shared, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
    },
    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.

Last modified