Skip to content

Commit c617cac

Browse files
authored
Update bridge/service-control sample to NServiceBus 10 (#7518)
* Add baseline Bridge_4 copy from Bridge_3 * Update bridge/service-control sample to NServiceBus 10
1 parent 9e3753c commit c617cac

File tree

13 files changed

+304
-0
lines changed

13 files changed

+304
-0
lines changed
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+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\Shared\Shared.csproj" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0-preview.6.*" />
14+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
15+
<PackageReference Include="NServiceBus.MessagingBridge" Version="4.0.0-alpha.1" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using NServiceBus;
5+
6+
Console.Title = "Bridge";
7+
8+
var builder = Host.CreateApplicationBuilder();
9+
10+
builder.Logging.ClearProviders();
11+
builder.Logging.AddSimpleConsole(options =>
12+
{
13+
options.IncludeScopes = false;
14+
options.SingleLine = true;
15+
options.TimestampFormat = "hh:mm:ss ";
16+
});
17+
18+
var bridgeConfiguration = new BridgeConfiguration();
19+
20+
#region ServiceControlTransport
21+
22+
var serviceControlTransport = new BridgeTransport(new LearningTransport());
23+
serviceControlTransport.HasEndpoint("Particular.ServiceControl");
24+
serviceControlTransport.HasEndpoint("Particular.Monitoring");
25+
serviceControlTransport.HasEndpoint("error");
26+
serviceControlTransport.HasEndpoint("audit");
27+
28+
#endregion
29+
30+
#region EndpointSideConfig
31+
32+
var learningTransport = new LearningTransport
33+
{
34+
// Set storage directory and add the character '2' to simulate a different transport.
35+
StorageDirectory = $"{LearningTransportInfrastructure.FindStoragePath()}2"
36+
};
37+
38+
var endpointsTransport = new BridgeTransport(learningTransport)
39+
{
40+
// A different name is required if transports are used twice.
41+
Name = "right-side"
42+
};
43+
44+
endpointsTransport.HasEndpoint("Samples.Bridge.Endpoint");
45+
46+
#endregion
47+
48+
bridgeConfiguration.AddTransport(serviceControlTransport);
49+
bridgeConfiguration.AddTransport(endpointsTransport);
50+
builder.UseNServiceBusBridge(bridgeConfiguration);
51+
52+
var host = builder.Build();
53+
54+
await host.RunAsync();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
<ProjectReference Include="..\Shared\Shared.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
15+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
16+
<PackageReference Include="NServiceBus.Heartbeat" Version="6.0.0-alpha.1" />
17+
<PackageReference Include="NServiceBus.Metrics.ServiceControl" Version="6.0.0-alpha.1" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
public static class FailureSimulator
5+
{
6+
public static bool Enabled { get; set; }
7+
8+
public static Task Invoke()
9+
{
10+
if (Enabled)
11+
{
12+
throw new Exception("Database is down");
13+
}
14+
return Task.CompletedTask;
15+
}
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using NServiceBus;
2+
3+
record MyMessage(string Id) : IMessage;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
using Microsoft.Extensions.Logging;
4+
5+
sealed class MyMessageHandler(ILogger<MyMessageHandler> logger) :
6+
IHandleMessages<MyMessage>
7+
{
8+
public Task Handle(MyMessage message, IMessageHandlerContext context)
9+
{
10+
logger.LogInformation("Processing message {MessageId}", message.Id);
11+
return FailureSimulator.Invoke();
12+
}
13+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using NServiceBus;
8+
9+
Console.Title = "Endpoint";
10+
11+
var host = Host.CreateDefaultBuilder(args)
12+
.UseNServiceBus(x =>
13+
{
14+
var endpointConfiguration = new EndpointConfiguration(
15+
"Samples.Bridge.Endpoint");
16+
17+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
18+
endpointConfiguration.UseTransport(new LearningTransport
19+
{
20+
StorageDirectory = $"{LearningTransportInfrastructure.FindStoragePath()}2"
21+
});
22+
23+
var recoverability = endpointConfiguration.Recoverability();
24+
recoverability.Immediate(
25+
customizations: immediate => { immediate.NumberOfRetries(0); });
26+
recoverability.Delayed(delayed => delayed.NumberOfRetries(0));
27+
28+
endpointConfiguration.SendFailedMessagesTo("error");
29+
endpointConfiguration.AuditProcessedMessagesTo("audit");
30+
endpointConfiguration.SendHeartbeatTo("Particular.ServiceControl");
31+
endpointConfiguration.EnableInstallers();
32+
endpointConfiguration.EnableMetrics()
33+
.SendMetricDataToServiceControl("Particular.Monitoring", TimeSpan.FromSeconds(1));
34+
35+
var routing = endpointConfiguration.UseTransport(new LearningTransport());
36+
routing.RouteToEndpoint(typeof(MyMessage), "Endpoint");
37+
38+
return endpointConfiguration;
39+
})
40+
.Build();
41+
42+
await host.StartAsync();
43+
44+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
45+
46+
const string letters = "ABCDEFGHIJKLMNOPQRSTUVXYZ";
47+
var random = new Random();
48+
Console.WriteLine("Press enter to exit");
49+
Console.WriteLine("Press 'o' to send a message");
50+
Console.WriteLine("Press 'f' to toggle simulating of message processing failure");
51+
52+
while (true)
53+
{
54+
var key = Console.ReadKey();
55+
Console.WriteLine();
56+
if (key.Key == ConsoleKey.Enter)
57+
{
58+
break;
59+
}
60+
var lowerInvariant = char.ToLowerInvariant(key.KeyChar);
61+
switch (lowerInvariant)
62+
{
63+
case 'o':
64+
var id = string.Concat(Enumerable.Range(0, 4).Select(x => letters[random.Next(letters.Length)]));
65+
var message = new MyMessage(id);
66+
await messageSession.Send(message);
67+
break;
68+
case 'f':
69+
FailureSimulator.Enabled = !FailureSimulator.Enabled;
70+
Console.WriteLine($"Failure simulation is now turned {(FailureSimulator.Enabled ? "on" : "off")}");
71+
break;
72+
}
73+
}
74+
75+
await host.StopAsync();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Particular.PlatformSample" Version="3.*" />
9+
</ItemGroup>
10+
11+
</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+
await Particular.PlatformLauncher.Launch();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.4.33122.133
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlatformLauncher", "PlatformLauncher\PlatformLauncher.csproj", "{8EDE58D6-68A4-4590-A383-6DFB7B482538}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Endpoint", "Endpoint\Endpoint.csproj", "{44CC7A25-B75E-4EAE-8DE9-DDC127270E4F}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{552A931D-AE0C-4127-B74E-CE6C76EF05E2}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{13195406-43BD-4108-AC5A-3D2474C229C9}"
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+
{8EDE58D6-68A4-4590-A383-6DFB7B482538}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{8EDE58D6-68A4-4590-A383-6DFB7B482538}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{8EDE58D6-68A4-4590-A383-6DFB7B482538}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{8EDE58D6-68A4-4590-A383-6DFB7B482538}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{44CC7A25-B75E-4EAE-8DE9-DDC127270E4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{44CC7A25-B75E-4EAE-8DE9-DDC127270E4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{44CC7A25-B75E-4EAE-8DE9-DDC127270E4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{44CC7A25-B75E-4EAE-8DE9-DDC127270E4F}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{552A931D-AE0C-4127-B74E-CE6C76EF05E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{552A931D-AE0C-4127-B74E-CE6C76EF05E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{552A931D-AE0C-4127-B74E-CE6C76EF05E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{552A931D-AE0C-4127-B74E-CE6C76EF05E2}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{13195406-43BD-4108-AC5A-3D2474C229C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{13195406-43BD-4108-AC5A-3D2474C229C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{13195406-43BD-4108-AC5A-3D2474C229C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{13195406-43BD-4108-AC5A-3D2474C229C9}.Release|Any CPU.Build.0 = Release|Any CPU
35+
EndGlobalSection
36+
GlobalSection(SolutionProperties) = preSolution
37+
HideSolutionNode = FALSE
38+
EndGlobalSection
39+
GlobalSection(ExtensibilityGlobals) = postSolution
40+
SolutionGuid = {064B3DE1-30E8-4668-8CD4-F238E85C14FB}
41+
EndGlobalSection
42+
EndGlobal

0 commit comments

Comments
 (0)