Skip to content

Commit f3c7610

Browse files
authored
Nsb10 samples command routing (#7706)
updated to nsb10
1 parent a6be96a commit f3c7610

File tree

11 files changed

+204
-0
lines changed

11 files changed

+204
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29728.190
5+
MinimumVisualStudioVersion = 15.0.26730.12
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sender", "Sender\Sender.csproj", "{9F8E556C-0AFA-4C5D-B64F-AE9376ABF145}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{20F613ED-C871-477C-B1E4-48B96CACF794}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receiver", "Receiver\Receiver.csproj", "{E2DF70C0-5A44-4358-AF90-C15DBAD978BF}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{9F8E556C-0AFA-4C5D-B64F-AE9376ABF145}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{9F8E556C-0AFA-4C5D-B64F-AE9376ABF145}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{20F613ED-C871-477C-B1E4-48B96CACF794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{20F613ED-C871-477C-B1E4-48B96CACF794}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{E2DF70C0-5A44-4358-AF90-C15DBAD978BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{E2DF70C0-5A44-4358-AF90-C15DBAD978BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus;
4+
5+
class CancelOrderHandler(ILogger<CancelOrderHandler> logger) :
6+
IHandleMessages<CancelOrder>
7+
{
8+
public Task Handle(CancelOrder message, IMessageHandlerContext context)
9+
{
10+
logger.LogInformation("CancelOrder command received: {OrderId}", message.OrderId);
11+
return Task.CompletedTask;
12+
}
13+
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus;
4+
class PlaceOrderHandler(ILogger<PlaceOrderHandler> logger) :
5+
IHandleMessages<PlaceOrder>
6+
{
7+
public Task Handle(PlaceOrder message, IMessageHandlerContext context)
8+
{
9+
logger.LogInformation("PlaceOrder command received: {OrderId} {Value}", message.OrderId, message.Value);
10+
return Task.CompletedTask;
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using Microsoft.Extensions.Hosting;
3+
using NServiceBus;
4+
5+
Console.Title = "Receiver";
6+
7+
var builder = Host.CreateApplicationBuilder(args);
8+
var endpointConfiguration = new EndpointConfiguration("Samples.CommandRouting.Receiver");
9+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10+
endpointConfiguration.UseTransport(new LearningTransport());
11+
12+
13+
Console.WriteLine("Press any key, the application is starting");
14+
Console.ReadKey();
15+
Console.WriteLine("Starting...");
16+
17+
builder.UseNServiceBus(endpointConfiguration);
18+
await builder.Build().RunAsync();
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+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using NServiceBus;
6+
7+
Console.Title = "Sender";
8+
9+
var builder = Host.CreateApplicationBuilder(args);
10+
11+
var endpointConfiguration = new EndpointConfiguration("Samples.CommandRouting.Sender");
12+
13+
#region configure-command-route
14+
var routing = endpointConfiguration.UseTransport(new LearningTransport());
15+
16+
routing.RouteToEndpoint(
17+
messageType: typeof(PlaceOrder),
18+
destination: "Samples.CommandRouting.Receiver"
19+
);
20+
#endregion
21+
22+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
23+
24+
builder.UseNServiceBus(endpointConfiguration);
25+
26+
var host = builder.Build();
27+
28+
Console.WriteLine("Press any key, the application is starting");
29+
Console.ReadKey();
30+
Console.WriteLine("Starting...");
31+
32+
await host.StartAsync();
33+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
34+
35+
Console.WriteLine("Press S to send an order");
36+
Console.WriteLine("Press C to cancel an order");
37+
Console.WriteLine("Press ESC to exit");
38+
39+
ConsoleKey keyPressed = Console.ReadKey(true).Key;
40+
41+
while (keyPressed != ConsoleKey.Escape)
42+
{
43+
switch (keyPressed)
44+
{
45+
case ConsoleKey.S:
46+
await PlaceOrder(messageSession, Guid.NewGuid().ToString(), 25m);
47+
break;
48+
case ConsoleKey.C:
49+
await CancelOrder(messageSession, Guid.NewGuid().ToString());
50+
break;
51+
}
52+
53+
keyPressed = Console.ReadKey(true).Key;
54+
}
55+
56+
await host.StopAsync();
57+
58+
static async Task PlaceOrder(IMessageSession messageSession, string orderId, decimal value)
59+
{
60+
#region send-command-with-configured-route
61+
var command = new PlaceOrder
62+
{
63+
OrderId = orderId,
64+
Value = value
65+
};
66+
67+
await messageSession.Send(command);
68+
Console.WriteLine($"Order placed: {orderId}");
69+
#endregion
70+
}
71+
72+
static async Task CancelOrder(IMessageSession messageSession, string orderId)
73+
{
74+
#region send-command-without-configured-route
75+
var command = new CancelOrder
76+
{
77+
OrderId = orderId
78+
};
79+
80+
await messageSession.Send("Samples.CommandRouting.Receiver", command);
81+
Console.WriteLine($"Order canceled: {orderId}");
82+
#endregion
83+
}
84+
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+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using NServiceBus;
2+
3+
public record CancelOrder :
4+
ICommand
5+
{
6+
public string OrderId { get; init; }
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using NServiceBus;
2+
public record PlaceOrder :
3+
ICommand
4+
{
5+
public string OrderId { get; init; }
6+
public decimal Value { get; init; }
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<LangVersion>preview</LangVersion>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)