Skip to content

Commit 2b024c9

Browse files
authored
Merge pull request #123417 from Particular/identity_and_update_latest
Identity and update to latest NServiceBus
2 parents 1ec1625 + 0bd6302 commit 2b024c9

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

articles/service-bus-messaging/build-message-driven-apps-nservicebus.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ The sample assumes you've [created an Azure Service Bus namespace](service-bus-q
3232
The following diagram, generated by [ServiceInsight](https://particular.net/serviceinsight), a visualization and debugging tool from Particular Software, shows the message flow:
3333

3434
:::image type="content" source="./media/nservicebus/sequence-diagram.png" alt-text="Image showing the sequence diagram":::
35-
1. Open `SendReceiveWithNservicebus.sln` in your favorite code editor (For example, Visual Studio 2019).
35+
1. Open `SendReceiveWithNservicebus.sln` in your favorite code editor (For example, Visual Studio 2022).
3636
1. Open `appsettings.json` in both the Receiver and Sender projects and set `AzureServiceBusConnectionString` to the connection string for your Azure Service Bus namespace.
37-
38-
37+
- This can be found in the Azure portal under **Service Bus Namespace** > **Settings** > **Shared access policies** > **RootManageSharedAccessKey** > **Primary Connection String** .
38+
- The `AzureServiceBusTransport` also has a constructor that accepts a namespace and token credential, which in a production environment will be more secure, however for the purposes of this tutorial the shared access key connection string will be used.
3939

4040
## Define the shared message contracts
4141

@@ -86,14 +86,15 @@ var host = Host.CreateDefaultBuilder(args)
8686
// Configure the NServiceBus endpoint
8787
var endpointConfiguration = new EndpointConfiguration("Sender");
8888

89-
var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
9089
var connectionString = context.Configuration.GetConnectionString("AzureServiceBusConnectionString");
91-
transport.ConnectionString(connectionString);
90+
// If token credentials are to be used, the overload constructor for AzureServiceBusTransport would be used here
91+
var routing = endpointConfiguration.UseTransport(new AzureServiceBusTransport(connectionString));
92+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
9293

93-
transport.Routing().RouteToEndpoint(typeof(Ping), "Receiver");
94+
endpointConfiguration.AuditProcessedMessagesTo("audit");
95+
routing.RouteToEndpoint(typeof(Ping), "Receiver");
9496

9597
endpointConfiguration.EnableInstallers();
96-
endpointConfiguration.AuditProcessedMessagesTo("audit");
9798

9899
return endpointConfiguration;
99100
})
@@ -140,13 +141,11 @@ public class SenderWorker : BackgroundService
140141
var round = 0;
141142
while (!stoppingToken.IsCancellationRequested)
142143
{
143-
await messageSession.Send(new Ping { Round = round++ })
144-
.ConfigureAwait(false);
144+
await messageSession.Send(new Ping { Round = round++ });;
145145

146146
logger.LogInformation($"Message #{round}");
147147

148-
await Task.Delay(1_000, stoppingToken)
149-
.ConfigureAwait(false);
148+
await Task.Delay(1_000, stoppingToken);
150149
}
151150
}
152151
catch (OperationCanceledException)
@@ -204,11 +203,10 @@ Let's ignore the commented code for now; we'll get back to it later when we talk
204203

205204
The class implements `IHandleMessages<Ping>`, which defines one method: `Handle`. This interface tells NServiceBus that when the endpoint receives a message of type `Ping`, it should be processed by the `Handle` method in this handler. The `Handle` method takes the message itself as a parameter, and an `IMessageHandlerContext`, which allows further messaging operations, such as replying, sending commands, or publishing events.
206205

207-
Our `PingHandler` is straightforward: when a `Ping` message is received, log the message details and reply back to the sender with a new `Pong` message.
206+
Our `PingHandler` is straightforward: when a `Ping` message is received, log the message details and reply back to the sender with a new `Pong` message, which is subsequently handled in the Sender's `PongHandler`.
208207

209208
> [!NOTE]
210209
> In the Sender's configuration, you specified that `Ping` messages should be routed to the Receiver. NServiceBus adds metadata to the messages indicating, among other things, the origin of the message. This is why you don't need to specify any routing data for the `Pong` reply message; it's automatically routed back to its origin: the Sender.
211-
>
212210
213211
With the Sender and Receiver both properly configured, you can now run the solution.
214212

@@ -301,21 +299,20 @@ Next, it's time to figure out where to deploy our solution in Azure.
301299
In this sample, the Sender and Receiver endpoints are configured to run as console applications. They can also be hosted in various Azure services including Azure Functions, Azure App Services, Azure Container Instances, Azure Kubernetes Services, and Azure VMs. For example, here's how the Sender endpoint can be configured to run as an Azure Function:
302300

303301
```csharp
304-
[assembly: FunctionsStartup(typeof(Startup))]
305-
[assembly: NServiceBusEndpointName("Sender")]
306-
307-
public class Startup : FunctionsStartup
302+
[assembly: NServiceBusTriggerFunction("Sender")]
303+
public class Program
308304
{
309-
public override void Configure(IFunctionsHostBuilder builder)
305+
public static async Task Main()
310306
{
311-
builder.UseNServiceBus(() =>
312-
{
313-
var configuration = new ServiceBusTriggeredEndpointConfiguration("Sender");
314-
var transport = configuration.AdvancedConfiguration.Transport;
315-
transport.Routing().RouteToEndpoint(typeof(Ping), "Receiver");
307+
var host = new HostBuilder()
308+
.ConfigureFunctionsWorkerDefaults()
309+
.UseNServiceBus(configuration =>
310+
{
311+
configuration.Routing().RouteToEndpoint(typeof(Ping), "Receiver");
312+
})
313+
.Build();
316314

317-
return configuration;
318-
});
315+
await host.RunAsync();
319316
}
320317
}
321318
```

0 commit comments

Comments
 (0)