Skip to content

Commit 12cf313

Browse files
authored
Consumer driven contracts core10 (#7593)
* Add Docker containerization for consumer-driven-contracts Core_9 sample - Add Dockerfile to build and run the sample in a container - Add run_sample.sh orchestration script that: - Starts Consumer1 and Consumer2 applications - Uses expect to automate Producer interaction (press 'p' to publish) - Verifies both consumers receive their respective contract messages - Provides clear success/failure feedback - Add necessary configuration files (nuget.config, Directory.Build.props, BannedSymbols.txt) - Sample successfully demonstrates consumer-driven contracts with automated verification * Add NServiceBus 10 version of consumer-driven-contracts sample (Core_10) - Duplicate Core_9 to create Core_10 - Update all project files to target .NET 10 with preview language version - Update NServiceBus packages to version 10.*-* (alpha from Feedz.io) - Update NServiceBus.Extensions.Hosting to version 4.*-* - Update Microsoft.Extensions.Logging.Console to version 10.*-* - Update Dockerfile to use .NET 10 preview SDK - Update run_sample.sh script to use --framework net10.0 - Add prerelease.txt marker file - Verified sample works correctly with automated testing in container * Modernize Core_10 sample with C# 12 features - Applied primary constructors to event handlers - Used file-scoped namespaces throughout - Enabled preview language features for C# 12 - Maintained NServiceBus compatibility by keeping traditional properties * Fix Dockerfile configuration file references and add build instructions - Remove duplicate nuget.config, Directory.Build.props, and BannedSymbols.txt from sample directories - Update Dockerfiles to reference configuration files from repository root - Add clear build instructions showing Docker/Podman commands must run from repository root - Build context now runs from repository root with proper file paths - Verified both Core_9 and Core_10 samples build and run correctly * Fix package reference versions * Fix targetframework attribute
1 parent 2397c0b commit 12cf313

File tree

17 files changed

+546
-0
lines changed

17 files changed

+546
-0
lines changed
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="NServiceBus" Version="10.0.0-alpha.1" />
9+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
10+
</ItemGroup>
11+
</Project>
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+
using Subscriber1.Contracts;
5+
6+
class Consumer1EventHandler(ILogger<Consumer1EventHandler> logger) : IHandleMessages<Consumer1Contract>
7+
{
8+
public Task Handle(Consumer1Contract message, IMessageHandlerContext context)
9+
{
10+
logger.LogInformation(message.Consumer1Property);
11+
return Task.CompletedTask;
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Subscriber1.Contracts;
2+
3+
using NServiceBus;
4+
5+
public interface Consumer1Contract : IEvent
6+
{
7+
string Consumer1Property { get; set; }
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
6+
class Program
7+
{
8+
public static async Task Main(string[] args)
9+
{
10+
await CreateHostBuilder(args).Build().RunAsync();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureServices((hostContext, services) =>
16+
{
17+
Console.Title = "Consumer1";
18+
}).UseNServiceBus(x =>
19+
{
20+
var endpointConfiguration = new EndpointConfiguration("Samples.ConsumerDrivenContracts.Consumer1");
21+
var transport = endpointConfiguration.UseTransport(new LearningTransport());
22+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
23+
24+
endpointConfiguration.SendFailedMessagesTo("error");
25+
endpointConfiguration.EnableInstallers();
26+
return endpointConfiguration;
27+
});
28+
}
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+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.0-preview.5.25277.114" />
9+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
</ItemGroup>
12+
</Project>
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+
using Subscriber2.Contracts;
5+
6+
class Consumer2EventHandler(ILogger<Consumer2EventHandler> logger) : IHandleMessages<Consumer2Contract>
7+
{
8+
public Task Handle(Consumer2Contract message, IMessageHandlerContext context)
9+
{
10+
logger.LogInformation(message.Consumer2Property);
11+
return Task.CompletedTask;
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Subscriber2.Contracts;
2+
3+
using NServiceBus;
4+
5+
public interface Consumer2Contract : IEvent
6+
{
7+
string Consumer2Property { get; set; }
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
6+
class Program
7+
{
8+
public static async Task Main(string[] args)
9+
{
10+
await CreateHostBuilder(args).Build().RunAsync();
11+
}
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureServices((hostContext, services) =>
16+
{
17+
Console.Title = "Consumer2";
18+
}).UseNServiceBus(x =>
19+
{
20+
var endpointConfiguration = new EndpointConfiguration("Samples.ConsumerDrivenContracts.Consumer2");
21+
var transport = endpointConfiguration.UseTransport(new LearningTransport());
22+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
23+
24+
endpointConfiguration.SendFailedMessagesTo("error");
25+
endpointConfiguration.EnableInstallers();
26+
return endpointConfiguration;
27+
});
28+
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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}") = "Producer", "Producer\Producer.csproj", "{B9D5FCC6-26EA-4863-B060-87A68031812C}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consumer1", "Consumer1\Consumer1.csproj", "{2DFF55AD-FF20-4A9B-85A7-1471A83FB8E7}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Consumer2", "Consumer2\Consumer2.csproj", "{A7BDDCF2-D335-44E9-9809-EFDFDCE6CAE8}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Debug|x64 = Debug|x64
16+
Debug|x86 = Debug|x86
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{B9D5FCC6-26EA-4863-B060-87A68031812C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{B9D5FCC6-26EA-4863-B060-87A68031812C}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{B9D5FCC6-26EA-4863-B060-87A68031812C}.Debug|x64.ActiveCfg = Debug|Any CPU
22+
{B9D5FCC6-26EA-4863-B060-87A68031812C}.Debug|x86.ActiveCfg = Debug|Any CPU
23+
{2DFF55AD-FF20-4A9B-85A7-1471A83FB8E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{2DFF55AD-FF20-4A9B-85A7-1471A83FB8E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{2DFF55AD-FF20-4A9B-85A7-1471A83FB8E7}.Debug|x64.ActiveCfg = Debug|Any CPU
26+
{2DFF55AD-FF20-4A9B-85A7-1471A83FB8E7}.Debug|x86.ActiveCfg = Debug|Any CPU
27+
{A7BDDCF2-D335-44E9-9809-EFDFDCE6CAE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{A7BDDCF2-D335-44E9-9809-EFDFDCE6CAE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{A7BDDCF2-D335-44E9-9809-EFDFDCE6CAE8}.Debug|x64.ActiveCfg = Debug|Any CPU
30+
{A7BDDCF2-D335-44E9-9809-EFDFDCE6CAE8}.Debug|x86.ActiveCfg = Debug|Any CPU
31+
EndGlobalSection
32+
GlobalSection(SolutionProperties) = preSolution
33+
HideSolutionNode = FALSE
34+
EndGlobalSection
35+
EndGlobal
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Build from repository root with:
2+
# podman build -f samples/consumer-driven-contracts/Core_10/Dockerfile -t sample-core10 .
3+
# docker build -f samples/consumer-driven-contracts/Core_10/Dockerfile -t sample-core10 .
4+
FROM mcr.microsoft.com/dotnet/sdk:10.0-preview
5+
6+
# Install expect for automated input
7+
RUN apt-get update && apt-get install -y expect procps && rm -rf /var/lib/apt/lists/*
8+
9+
# Set working directory
10+
WORKDIR /app
11+
12+
# Copy configuration files from repository root
13+
COPY nuget.config ./
14+
COPY Directory.Build.props ./
15+
COPY BannedSymbols.txt ./
16+
17+
# Copy solution and project files
18+
COPY samples/consumer-driven-contracts/Core_10/*.sln ./
19+
COPY samples/consumer-driven-contracts/Core_10/Consumer1/*.csproj Consumer1/
20+
COPY samples/consumer-driven-contracts/Core_10/Consumer2/*.csproj Consumer2/
21+
COPY samples/consumer-driven-contracts/Core_10/Producer/*.csproj Producer/
22+
23+
# Restore packages
24+
RUN dotnet restore
25+
26+
# Copy all source code
27+
COPY samples/consumer-driven-contracts/Core_10/ .
28+
29+
# Restore again to ensure all packages are available after copying all files
30+
RUN dotnet restore
31+
32+
# Build the solution using the latest framework
33+
RUN dotnet build --no-restore --framework net10.0
34+
35+
CMD ["/app/run_sample.sh"]

0 commit comments

Comments
 (0)