Skip to content

Commit fcdc406

Browse files
Add ErrorHandling sample for .NET 10 with NServiceBus 10 and modern C… (#7541)
* Add ErrorHandling sample for .NET 10 with NServiceBus 10 and modern C# features
1 parent 2870a8b commit fcdc406

File tree

12 files changed

+226
-0
lines changed

12 files changed

+226
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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}") = "Shared", "Shared\Shared.csproj", "{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WithDelayedRetries", "WithDelayedRetries\WithDelayedRetries.csproj", "{B2C3D4E5-F6A7-89BC-DEF0-123456789ABC}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WithoutDelayedRetries", "WithoutDelayedRetries\WithoutDelayedRetries.csproj", "{C3D4E5F6-A789-BCDE-F012-3456789ABCDE}"
10+
EndProject
11+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlatformLauncher", "PlatformLauncher\PlatformLauncher.csproj", "{D4E5F6A7-89BC-DEF0-1234-56789ABCDEF0}"
12+
EndProject
13+
Global
14+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
15+
Debug|Any CPU = Debug|Any CPU
16+
Release|Any CPU = Release|Any CPU
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{A1B2C3D4-E5F6-789A-BCDE-F0123456789A}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{B2C3D4E5-F6A7-89BC-DEF0-123456789ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{B2C3D4E5-F6A7-89BC-DEF0-123456789ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{B2C3D4E5-F6A7-89BC-DEF0-123456789ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{B2C3D4E5-F6A7-89BC-DEF0-123456789ABC}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{C3D4E5F6-A789-BCDE-F012-3456789ABCDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{C3D4E5F6-A789-BCDE-F012-3456789ABCDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{C3D4E5F6-A789-BCDE-F012-3456789ABCDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{C3D4E5F6-A789-BCDE-F012-3456789ABCDE}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{D4E5F6A7-89BC-DEF0-1234-56789ABCDEF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{D4E5F6A7-89BC-DEF0-1234-56789ABCDEF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{D4E5F6A7-89BC-DEF0-1234-56789ABCDEF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{D4E5F6A7-89BC-DEF0-1234-56789ABCDEF0}.Release|Any CPU.Build.0 = Release|Any CPU
35+
EndGlobalSection
36+
GlobalSection(SolutionProperties) = preSolution
37+
HideSolutionNode = FALSE
38+
EndGlobalSection
39+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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="Particular.PlatformSample" Version="2.*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System;
2+
3+
Console.Title = "PlatformLauncher";
4+
Particular.PlatformLauncher.Launch();
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 : ICommand
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Threading.Tasks;
4+
using NServiceBus;
5+
6+
public class MyMessageHandler : IHandleMessages<MyMessage>
7+
{
8+
static readonly ConcurrentDictionary<Guid, string> last = new();
9+
10+
public Task Handle(MyMessage message, IMessageHandlerContext context)
11+
{
12+
Console.WriteLine($"Handling {nameof(MyMessage)} with MessageId:{context.MessageId}");
13+
14+
if (context.MessageHeaders.TryGetValue(Headers.DelayedRetries, out var numOfRetries))
15+
{
16+
last.TryGetValue(message.Id, out var value);
17+
18+
if (numOfRetries != value)
19+
{
20+
Console.WriteLine($"This is retry number {numOfRetries}");
21+
last.AddOrUpdate(message.Id, numOfRetries, (key, oldValue) => numOfRetries);
22+
}
23+
}
24+
25+
throw new Exception("An exception occurred in the handler.");
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
5+
Console.Title = "WithDelayedRetries";
6+
7+
var endpointConfiguration = new EndpointConfiguration("Samples.ErrorHandling.WithDelayedRetries");
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 that will throw an exception.");
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 myMessage = new MyMessage
26+
{
27+
Id = Guid.NewGuid()
28+
};
29+
30+
await endpointInstance.SendLocal(myMessage);
31+
}
32+
33+
await endpointInstance.Stop();
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
5+
#region Handler
6+
public class MyMessageHandler : IHandleMessages<MyMessage>
7+
{
8+
public Task Handle(MyMessage message, IMessageHandlerContext context)
9+
{
10+
Console.WriteLine($"Handling {nameof(MyMessage)} with MessageId:{context.MessageId}");
11+
throw new Exception("An exception occurred in the handler.");
12+
}
13+
}
14+
#endregion
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NServiceBus;
4+
5+
Console.Title = "WithoutDelayedRetries";
6+
7+
#region Disable
8+
var endpointConfiguration = new EndpointConfiguration("Samples.ErrorHandling.WithoutDelayedRetries");
9+
var recoverability = endpointConfiguration.Recoverability();
10+
recoverability.Delayed(
11+
customizations: delayed =>
12+
{
13+
delayed.NumberOfRetries(0);
14+
});
15+
#endregion
16+
17+
endpointConfiguration.UsePersistence<LearningPersistence>();
18+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
19+
endpointConfiguration.UseTransport(new LearningTransport());
20+
21+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
22+
23+
Console.WriteLine("Press enter to send a message that will throw an exception.");
24+
Console.WriteLine("Press any key to exit");
25+
26+
while (true)
27+
{
28+
var key = Console.ReadKey();
29+
if (key.Key != ConsoleKey.Enter)
30+
{
31+
break;
32+
}
33+
34+
var myMessage = new MyMessage
35+
{
36+
Id = Guid.NewGuid()
37+
};
38+
39+
await endpointInstance.SendLocal(myMessage);
40+
}
41+
42+
await endpointInstance.Stop();

0 commit comments

Comments
 (0)