Skip to content

Commit 2870a8b

Browse files
Add FaultTolerance sample for .NET 10 with NServiceBus 10 and modern C# features (#7540)
1 parent 480aa2a commit 2870a8b

File tree

9 files changed

+169
-0
lines changed

9 files changed

+169
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>preview</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\Shared\Shared.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
5+
Console.Title = "Client";
6+
7+
var endpointConfiguration = new EndpointConfiguration("Samples.FaultTolerance.Client");
8+
endpointConfiguration.UsePersistence<LearningPersistence>();
9+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10+
endpointConfiguration.UseTransport(new LearningTransport());
11+
12+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
13+
14+
Console.WriteLine("Press enter to send a message");
15+
Console.WriteLine("Press any key to exit");
16+
17+
while (true)
18+
{
19+
var key = Console.ReadKey();
20+
if (key.Key != ConsoleKey.Enter)
21+
{
22+
break;
23+
}
24+
25+
var id = Guid.NewGuid();
26+
var myMessage = new MyMessage
27+
{
28+
Id = id
29+
};
30+
31+
await endpointInstance.Send("Samples.FaultTolerance.Server", myMessage);
32+
Console.WriteLine($"Sent a message with id: {id:N}");
33+
}
34+
35+
await endpointInstance.Stop();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.31903.59
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{C7F7D659-2E6B-4F1E-9C4C-2E6B4F1E9C4C}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}"
10+
EndProject
11+
Global
12+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
13+
Debug|Any CPU = Debug|Any CPU
14+
Release|Any CPU = Release|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{C7F7D659-2E6B-4F1E-9C4C-2E6B4F1E9C4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{C7F7D659-2E6B-4F1E-9C4C-2E6B4F1E9C4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{C7F7D659-2E6B-4F1E-9C4C-2E6B4F1E9C4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{C7F7D659-2E6B-4F1E-9C4C-2E6B4F1E9C4C}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
EndGlobal
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus;
4+
5+
#region MyHandler
6+
public class MyHandler(ILogger<MyHandler> logger) :
7+
IHandleMessages<MyMessage>
8+
{
9+
public Task Handle(MyMessage message, IMessageHandlerContext context)
10+
{
11+
logger.LogInformation("Message received. Id: {Id}", message.Id);
12+
// throw new Exception("Uh oh - something went wrong....");
13+
return Task.CompletedTask;
14+
}
15+
}
16+
#endregion
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
6+
Console.Title = "Server";
7+
8+
var builder = Host.CreateApplicationBuilder(args);
9+
10+
var endpointConfiguration = new EndpointConfiguration("Samples.FaultTolerance.Server");
11+
endpointConfiguration.UsePersistence<LearningPersistence>();
12+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
13+
endpointConfiguration.UseTransport(new LearningTransport());
14+
15+
#region disable
16+
//var recoverability = endpointConfiguration.Recoverability();
17+
//recoverability.Delayed(settings =>
18+
//{
19+
// settings.NumberOfRetries(0);
20+
//});
21+
#endregion
22+
23+
Console.WriteLine("Press any key, the application is starting");
24+
Console.ReadKey();
25+
Console.WriteLine("Starting...");
26+
27+
builder.UseNServiceBus(endpointConfiguration);
28+
29+
using var app = builder.Build();
30+
await app.RunAsync();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<LangVersion>preview</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\Shared\Shared.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
using NServiceBus;
3+
4+
public record MyMessage : IMessage
5+
{
6+
public required Guid Id { get; init; }
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prerelease

0 commit comments

Comments
 (0)