Skip to content

Commit a51c7bc

Browse files
authored
Add pubsub/native sample for NServiceBus 10 (#7595)
* Remove InputLoopService from pubsub/native Core_9 sample and integrate logic into Program.cs * Add Dockerfile and automated testing script for pubsub/native Core_9 sample * Add pubsub/native Core_10 sample for NServiceBus 10 with .NET 10
1 parent d0203d2 commit a51c7bc

File tree

16 files changed

+685
-57
lines changed

16 files changed

+685
-57
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Build context should be from the repository root
2+
# Example build commands:
3+
# docker build -f samples/pubsub/native/Core_10/Dockerfile -t pubsub-native-core10 .
4+
# podman build -f samples/pubsub/native/Core_10/Dockerfile -t pubsub-native-core10 .
5+
6+
FROM mcr.microsoft.com/dotnet/sdk:10.0-preview AS build
7+
WORKDIR /src
8+
9+
# Set environment variable for .NET 10 preview builds
10+
ENV DOTNET_SYSTEM_NET_SECURITY_NOREVOCATIONCHECKBYDEFAULT=true
11+
12+
# Copy necessary build files from repository root
13+
COPY nuget.config .
14+
COPY Directory.Build.props .
15+
COPY BannedSymbols.txt .
16+
17+
# Copy the sample solution
18+
COPY samples/pubsub/native/Core_10/ ./samples/pubsub/native/Core_10/
19+
20+
# Build the solution
21+
WORKDIR /src/samples/pubsub/native/Core_10
22+
RUN dotnet restore
23+
RUN dotnet publish Publisher/Publisher.csproj -f net10.0 -o /app/publisher --no-restore
24+
RUN dotnet publish Subscriber/Subscriber.csproj -f net10.0 -o /app/subscriber --no-restore
25+
26+
FROM mcr.microsoft.com/dotnet/runtime:10.0-preview AS runtime
27+
WORKDIR /app
28+
29+
# Set environment variable for .NET 10 preview runtime
30+
ENV DOTNET_SYSTEM_NET_SECURITY_NOREVOCATIONCHECKBYDEFAULT=true
31+
32+
# Install expect and process management tools
33+
RUN apt-get update && apt-get install -y expect procps && rm -rf /var/lib/apt/lists/*
34+
35+
# Create .learningtransport directory for LearningTransport storage
36+
RUN mkdir -p .learningtransport
37+
38+
# Copy published applications
39+
COPY --from=build /app/publisher ./publisher/
40+
COPY --from=build /app/subscriber ./subscriber/
41+
42+
# Copy startup script
43+
COPY samples/pubsub/native/Core_10/run_sample.sh ./
44+
RUN chmod +x run_sample.sh
45+
46+
CMD ["./run_sample.sh"]
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}") = "Shared", "Shared\Shared.csproj", "{5686FE6C-A5E3-40D1-A6BD-25F94DA612F8}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Publisher", "Publisher\Publisher.csproj", "{7036A49B-359F-4BC7-AFBA-DE3C7AB41986}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Subscriber", "Subscriber\Subscriber.csproj", "{6A699A4E-F2FD-4B71-AF73-199B499482BD}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{5686FE6C-A5E3-40D1-A6BD-25F94DA612F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{5686FE6C-A5E3-40D1-A6BD-25F94DA612F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{7036A49B-359F-4BC7-AFBA-DE3C7AB41986}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{7036A49B-359F-4BC7-AFBA-DE3C7AB41986}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{6A699A4E-F2FD-4B71-AF73-199B499482BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{6A699A4E-F2FD-4B71-AF73-199B499482BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 = "Publisher";
8+
var builder = Host.CreateApplicationBuilder(args);
9+
10+
var endpointConfiguration = new EndpointConfiguration("Samples.PubSub.Publisher");
11+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
12+
endpointConfiguration.UseTransport(new LearningTransport());
13+
14+
builder.UseNServiceBus(endpointConfiguration);
15+
16+
var host = builder.Build();
17+
18+
await host.StartAsync();
19+
20+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
21+
22+
Console.WriteLine("Press '1' to publish the OrderReceived event");
23+
Console.WriteLine("Press any other key to exit");
24+
25+
#region PublishLoop
26+
27+
while (true)
28+
{
29+
var key = Console.ReadKey();
30+
Console.WriteLine();
31+
32+
var orderReceivedId = Guid.NewGuid();
33+
if (key.Key == ConsoleKey.D1)
34+
{
35+
var orderReceived = new OrderReceived
36+
{
37+
OrderId = orderReceivedId
38+
};
39+
await messageSession.Publish(orderReceived);
40+
Console.WriteLine($"Published OrderReceived Event with Id {orderReceivedId}.");
41+
}
42+
else
43+
{
44+
break;
45+
}
46+
}
47+
48+
#endregion
49+
50+
await host.StopAsync();
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
using NServiceBus;
3+
4+
public class OrderReceived :
5+
IEvent
6+
{
7+
public Guid OrderId { get; set; }
8+
}
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.2" />
8+
</ItemGroup>
9+
</Project>
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+
public class OrderReceivedHandler (ILogger<OrderReceivedHandler> logger):
5+
IHandleMessages<OrderReceived>
6+
{
7+
public Task Handle(OrderReceived message, IMessageHandlerContext context)
8+
{
9+
logger.LogInformation("Subscriber has received OrderReceived event with OrderId {OrderId}.", message.OrderId);
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 System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
using NServiceBus.Routing;
6+
7+
8+
Console.Title = "Subscriber";
9+
var builder = Host.CreateApplicationBuilder(args);
10+
11+
var endpointConfiguration = new EndpointConfiguration("Samples.PubSub.Subscriber");
12+
13+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
14+
endpointConfiguration.UseTransport(new LearningTransport());
15+
16+
builder.UseNServiceBus(endpointConfiguration);
17+
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>

samples/pubsub/native/Core_10/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)