# Gateway ## 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/` ```cs var gatewayConfig = endpointConfiguration.Gateway(new NonDurableDeduplicationConfiguration()); gatewayConfig.AddReceiveChannel("http://localhost:25899/Headquarters/"); gatewayConfig.AddSite("RemoteSite", "http://localhost:25899/RemoteSite/"); ``` ## AcknowledgedHandler Handles `PriceUpdateAcknowledged` from `RemoteSite`. ```cs public class AcknowledgedHandler(ILogger logger) : IHandleMessages { public Task Handle(PriceUpdateAcknowledged message, IMessageHandlerContext context) { logger.LogInformation("Price update received by: {BranchOffice}", message.BranchOffice); return Task.CompletedTask; } } ``` ## UpdatePriceHandler Handles `UpdatePrice` from `WebClient`. ```cs public class UpdatePriceHandler(ILogger logger) : IHandleMessages { public Task Handle(UpdatePrice message, IMessageHandlerContext context) { logger.LogInformation("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/` ```cs var gatewayConfig = endpointConfiguration.Gateway(new NonDurableDeduplicationConfiguration()); gatewayConfig.AddReceiveChannel("http://localhost:25899/RemoteSite/"); ``` ### PriceUpdatedHandler ```cs public class PriceUpdatedHandler(ILogger logger) : IHandleMessages { public Task Handle(PriceUpdated message, IMessageHandlerContext context) { var header = context.MessageHeaders[Headers.OriginatingSite]; logger.LogInformation("Price update for: {ProductId} received. Reply over channel: {Channel}", message.ProductId, header); var updateAcknowledged = new PriceUpdateAcknowledged { BranchOffice = "RemoteSite" }; return context.Reply(updateAcknowledged); } } ``` ## WebClient This project represents an external integration point. It sends an `UpdatePrice` message to the channel `http://localhost:25899/Headquarters/`. ```html var message = "23"; 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); } }); ```