Skip to content

Commit cec9b6c

Browse files
authored
Added nsb10 sample (#7617)
1 parent 02b4a64 commit cec9b6c

14 files changed

+259
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.Azure.WebJobs;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
using Microsoft.Extensions.Logging;
11+
12+
using NServiceBus;
13+
14+
public static class HostBuilderExtensions
15+
{
16+
public static IHostBuilder ConfigureHost(this IHostBuilder hostBuilder)
17+
{
18+
hostBuilder.ConfigureWebJobs(builder => builder.AddAzureStorageCoreServices());
19+
20+
hostBuilder.ConfigureLogging((context, builder) =>
21+
{
22+
builder.AddConsole();
23+
});
24+
25+
#region WebJobHost_Start
26+
hostBuilder.UseNServiceBus(ctx =>
27+
{
28+
var endpointConfiguration = new EndpointConfiguration("receiver");
29+
endpointConfiguration.DefineCriticalErrorAction(OnCriticalError);
30+
endpointConfiguration.UsePersistence<NonDurablePersistence>();
31+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
32+
endpointConfiguration.EnableInstallers();
33+
34+
var transportConnectionString = ctx.Configuration.GetConnectionString("TransportConnectionString");
35+
endpointConfiguration.UseTransport(new AzureStorageQueueTransport(transportConnectionString));
36+
37+
return endpointConfiguration;
38+
});
39+
#endregion
40+
41+
hostBuilder.ConfigureServices((context, services) =>
42+
{
43+
services.AddHostedService<SimulateWorkHostedService>();
44+
services.AddSingleton<IJobHost, NoOpJobHost>();
45+
});
46+
47+
return hostBuilder;
48+
}
49+
50+
#region WebJobHost_CriticalError
51+
static async Task OnCriticalError(ICriticalErrorContext context, CancellationToken cancellationToken)
52+
{
53+
var fatalMessage =
54+
$"The following critical error was encountered:{Environment.NewLine}{context.Error}{Environment.NewLine}Process is shutting down. StackTrace: {Environment.NewLine}{context.Exception.StackTrace}";
55+
56+
try
57+
{
58+
await context.Stop(cancellationToken);
59+
}
60+
finally
61+
{
62+
Environment.FailFast(fatalMessage, context.Exception);
63+
}
64+
}
65+
#endregion
66+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using NServiceBus;
2+
public class MyMessage : ICommand
3+
{
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus;
4+
5+
public class MyMessageHandler : IHandleMessages<MyMessage>
6+
{
7+
public MyMessageHandler(ILogger<MyMessageHandler> logger)
8+
{
9+
this.logger = logger;
10+
}
11+
12+
public Task Handle(MyMessage message, IMessageHandlerContext context)
13+
{
14+
logger.LogInformation("Received message");
15+
return Task.CompletedTask;
16+
}
17+
18+
private readonly ILogger logger;
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Azure.WebJobs;
5+
6+
public class NoOpJobHost : IJobHost
7+
{
8+
public Task CallAsync(string name, IDictionary<string, object> arguments = null, CancellationToken cancellationToken = new CancellationToken())
9+
{
10+
throw new System.NotImplementedException();
11+
}
12+
13+
public Task StartAsync(CancellationToken cancellationToken)
14+
{
15+
return Task.CompletedTask;
16+
}
17+
18+
public Task StopAsync()
19+
{
20+
return Task.CompletedTask;
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.Azure.WebJobs;
2+
using Microsoft.Extensions.Hosting;
3+
4+
5+
var host = Host.CreateDefaultBuilder()
6+
.ConfigureHost()
7+
.Build();
8+
9+
var cancellationToken = new WebJobsShutdownWatcher().Token;
10+
using (host)
11+
{
12+
await host.RunAsync(cancellationToken);
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Receiver": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"DOTNET_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
<TargetFramework>net10.0</TargetFramework>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.*" />
11+
<PackageReference Include="Microsoft.Azure.WebJobs.Host.Storage" Version="5.*" />
12+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.2" />
13+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.2" />
14+
<PackageReference Include="NServiceBus.Persistence.NonDurable" Version="3.0.0-alpha.2" />
15+
<PackageReference Include="NServiceBus.Transport.AzureStorageQueues" Version="14.0.0-alpha.2" />
16+
</ItemGroup>
17+
18+
<ItemGroup Label="Resolves vulnerabilities">
19+
<PackageReference Include="Azure.Identity" Version="1.*" />
20+
<PackageReference Include="Microsoft.Identity.Client" Version="4.*" />
21+
</ItemGroup>
22+
23+
<ItemGroup Label="To disable Dashboard logging (https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started#enable-console-logging)">
24+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.0-preview.6.25358.103" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<None Update="appsettings.development.json" CopyToOutputDirectory="PreserveNewest" />
29+
<None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
30+
<None Update="Settings.job" CopyToOutputDirectory="PreserveNewest" />
31+
<None Update="run.cmd" CopyToOutputDirectory="PreserveNewest" />
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stopping_wait_time": 60
3+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Hosting;
5+
using NServiceBus;
6+
7+
class SimulateWorkHostedService : IHostedService
8+
{
9+
public SimulateWorkHostedService(IMessageSession messageSession)
10+
{
11+
this.messageSession = messageSession;
12+
}
13+
14+
public Task StartAsync(CancellationToken cancellationToken)
15+
{
16+
worker = SimulateWork(cancellationTokenSource.Token);
17+
return Task.CompletedTask;
18+
}
19+
20+
public Task StopAsync(CancellationToken cancellationToken)
21+
{
22+
cancellationTokenSource.Cancel();
23+
return worker;
24+
}
25+
26+
async Task SimulateWork(CancellationToken cancellationToken)
27+
{
28+
try
29+
{
30+
while (!cancellationToken.IsCancellationRequested)
31+
{
32+
// sending here to simulate work
33+
await messageSession.SendLocal(new MyMessage(), cancellationToken: cancellationToken);
34+
35+
await Task.Delay(1000, cancellationToken);
36+
}
37+
}
38+
catch (OperationCanceledException)
39+
{
40+
// graceful shutdown
41+
}
42+
}
43+
44+
readonly IMessageSession messageSession;
45+
readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
46+
Task worker;
47+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"ConnectionStrings": {
3+
"TransportConnectionString": "UseDevelopmentStorage=true"
4+
},
5+
"Logging": {
6+
"Console": {
7+
"LogLevel": {
8+
"Default": "Information",
9+
"Azure": "Warning"
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)