Skip to content

Commit 2599add

Browse files
authored
Multiple deserializers sample for NServiceBus 10 (#7570)
* Fixes for NSB 9 version * Duplicate of previous version with .NET 10 * Bump dependencies
1 parent 032efb0 commit 2599add

File tree

29 files changed

+544
-100
lines changed

29 files changed

+544
-100
lines changed
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+
<ProjectReference Include="..\Shared\Shared.csproj" />
9+
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.*" />
10+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
11+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="5.0.0-alpha.2" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Newtonsoft.Json.Bson;
5+
using NServiceBus;
6+
7+
Console.Title = "NewtonsoftBsonEndpoint";
8+
9+
#region configExternalNewtonsoftBson
10+
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ExternalNewtonsoftBsonEndpoint");
11+
12+
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
13+
14+
serialization.ReaderCreator(stream => new BsonDataReader(stream));
15+
serialization.WriterCreator(stream => new BsonDataWriter(stream));
16+
serialization.ContentTypeKey("NewtonsoftBson");
17+
18+
endpointConfiguration.RegisterOutgoingMessageLogger();
19+
20+
#endregion
21+
endpointConfiguration.UseTransport(new LearningTransport());
22+
23+
var builder = Host.CreateApplicationBuilder(args);
24+
builder.UseNServiceBus(endpointConfiguration);
25+
26+
var host = builder.Build();
27+
await host.StartAsync();
28+
29+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
30+
var message = MessageBuilder.BuildMessage();
31+
await messageSession.Send("Samples.MultipleDeserializers.ReceivingEndpoint", message);
32+
33+
Console.WriteLine("Order Sent");
34+
Console.WriteLine("Press any key to exit");
35+
Console.ReadKey();
36+
37+
await host.StopAsync();
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+
<ProjectReference Include="..\Shared\Shared.csproj" />
9+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
10+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="5.0.0-alpha.2" />
11+
</ItemGroup>
12+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using NServiceBus;
5+
6+
Console.Title = "NewtonsoftJsonEndpoint";
7+
8+
#region configExternalNewtonsoftJson
9+
10+
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ExternalNewtonsoftJsonEndpoint");
11+
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
12+
serialization.ContentTypeKey("NewtonsoftJson");
13+
endpointConfiguration.RegisterOutgoingMessageLogger();
14+
15+
#endregion
16+
endpointConfiguration.UseTransport(new LearningTransport());
17+
18+
var builder = Host.CreateApplicationBuilder(args);
19+
builder.UseNServiceBus(endpointConfiguration);
20+
21+
var host = builder.Build();
22+
await host.StartAsync();
23+
24+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
25+
26+
var message = MessageBuilder.BuildMessage();
27+
28+
await messageSession.Send("Samples.MultipleDeserializers.ReceivingEndpoint", message);
29+
30+
Console.WriteLine("Order Sent");
31+
Console.WriteLine("Press any key to exit");
32+
Console.ReadKey();
33+
34+
await host.StopAsync();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.9.34518.117
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XmlEndpoint", "XmlEndpoint\XmlEndpoint.csproj", "{A2202E81-B499-4038-AFE2-1846E904C61E}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReceivingEndpoint", "ReceivingEndpoint\ReceivingEndpoint.csproj", "{A2E753F8-1E10-4C79-B45F-280D8D5141CF}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{172110CC-3A13-4EA5-9A76-FCE2554CAE90}"
10+
EndProject
11+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExternalNewtonsoftBsonEndpoint", "ExternalNewtonsoftBsonEndpoint\ExternalNewtonsoftBsonEndpoint.csproj", "{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}"
12+
EndProject
13+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemJsonEndpoint", "SystemJsonEndpoint\SystemJsonEndpoint.csproj", "{BDE4DFBD-42A1-44F5-B2CB-418470E4E091}"
14+
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Senders", "Senders", "{216444CF-5BAA-450D-93ED-E45CC17B65F0}"
16+
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExternalNewtonsoftJsonEndpoint", "ExternalNewtonsoftJsonEndpoint\ExternalNewtonsoftJsonEndpoint.csproj", "{7113E88D-14C0-442B-89C3-4C74DFA1AA07}"
18+
EndProject
19+
Global
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Release|Any CPU = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{A2202E81-B499-4038-AFE2-1846E904C61E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{A2202E81-B499-4038-AFE2-1846E904C61E}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{A2202E81-B499-4038-AFE2-1846E904C61E}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{A2202E81-B499-4038-AFE2-1846E904C61E}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{A2E753F8-1E10-4C79-B45F-280D8D5141CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{A2E753F8-1E10-4C79-B45F-280D8D5141CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{A2E753F8-1E10-4C79-B45F-280D8D5141CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{A2E753F8-1E10-4C79-B45F-280D8D5141CF}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{172110CC-3A13-4EA5-9A76-FCE2554CAE90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{172110CC-3A13-4EA5-9A76-FCE2554CAE90}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{172110CC-3A13-4EA5-9A76-FCE2554CAE90}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{172110CC-3A13-4EA5-9A76-FCE2554CAE90}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{BDE4DFBD-42A1-44F5-B2CB-418470E4E091}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{BDE4DFBD-42A1-44F5-B2CB-418470E4E091}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{BDE4DFBD-42A1-44F5-B2CB-418470E4E091}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{BDE4DFBD-42A1-44F5-B2CB-418470E4E091}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{7113E88D-14C0-442B-89C3-4C74DFA1AA07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46+
{7113E88D-14C0-442B-89C3-4C74DFA1AA07}.Debug|Any CPU.Build.0 = Debug|Any CPU
47+
{7113E88D-14C0-442B-89C3-4C74DFA1AA07}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{7113E88D-14C0-442B-89C3-4C74DFA1AA07}.Release|Any CPU.Build.0 = Release|Any CPU
49+
EndGlobalSection
50+
GlobalSection(SolutionProperties) = preSolution
51+
HideSolutionNode = FALSE
52+
EndGlobalSection
53+
GlobalSection(NestedProjects) = preSolution
54+
{A2202E81-B499-4038-AFE2-1846E904C61E} = {216444CF-5BAA-450D-93ED-E45CC17B65F0}
55+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7} = {216444CF-5BAA-450D-93ED-E45CC17B65F0}
56+
{BDE4DFBD-42A1-44F5-B2CB-418470E4E091} = {216444CF-5BAA-450D-93ED-E45CC17B65F0}
57+
{7113E88D-14C0-442B-89C3-4C74DFA1AA07} = {216444CF-5BAA-450D-93ED-E45CC17B65F0}
58+
EndGlobalSection
59+
GlobalSection(ExtensibilityGlobals) = postSolution
60+
SolutionGuid = {133298DB-A504-4020-8F57-74DA9C9BAECC}
61+
EndGlobalSection
62+
EndGlobal
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 CreateOrderHandler(ILogger<CreateOrderHandler> logger) :
5+
IHandleMessages<CreateOrder>
6+
{
7+
public Task Handle(CreateOrder message, IMessageHandlerContext context)
8+
{
9+
logger.LogInformation("Order received. OriginatingEndpoint:" + context.MessageHeaders[Headers.OriginatingEndpoint]);
10+
return Task.CompletedTask;
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus.MessageMutator;
3+
4+
public interface IIncomingMessageBodyWriter
5+
{
6+
Task MutateIncoming(MutateIncomingTransportMessageContext context);
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Text;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using NServiceBus;
5+
using NServiceBus.MessageMutator;
6+
7+
#region incomingmutator
8+
public class IncomingMessageBodyWriter(ILogger<IncomingMessageBodyWriter> logger) :
9+
IMutateIncomingTransportMessages, IIncomingMessageBodyWriter
10+
{
11+
public Task MutateIncoming(MutateIncomingTransportMessageContext context)
12+
{
13+
var bodyAsString = Encoding.UTF8
14+
.GetString(context.Body.ToArray());
15+
var contentType = context.Headers[Headers.ContentType];
16+
17+
logger.LogInformation("ContentType '{ContentType}'. Serialized Message Body:\r\n{Body}", contentType, bodyAsString);
18+
return Task.CompletedTask;
19+
}
20+
}
21+
#endregion
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
4+
using Newtonsoft.Json.Bson;
5+
using NServiceBus;
6+
using NServiceBus.MessageMutator;
7+
8+
Console.Title = "ReceivingEndpoint";
9+
10+
var builder = Host.CreateApplicationBuilder(args);
11+
12+
#region configAll
13+
14+
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ReceivingEndpoint");
15+
16+
// Xml
17+
endpointConfiguration.UseSerialization<XmlSerializer>();
18+
19+
// Json
20+
endpointConfiguration.AddDeserializer<SystemJsonSerializer>();
21+
22+
// External Newtonsoft Json
23+
var externalNewtonsoftJson = endpointConfiguration.AddDeserializer<NewtonsoftJsonSerializer>();
24+
externalNewtonsoftJson.ContentTypeKey("NewtonsoftJson");
25+
26+
// External Newtonsoft Bson
27+
var externalNewtonsoftBson = endpointConfiguration.AddDeserializer<NewtonsoftJsonSerializer>();
28+
externalNewtonsoftBson.ReaderCreator(stream => new BsonDataReader(stream));
29+
externalNewtonsoftBson.WriterCreator(stream => new BsonDataWriter(stream));
30+
externalNewtonsoftBson.ContentTypeKey("NewtonsoftBson");
31+
32+
// Register the mutator so the message on the wire is written
33+
builder.Services.AddSingleton<IncomingMessageBodyWriter>();
34+
35+
var serviceProvider = builder.Services.BuildServiceProvider();
36+
var incomingMessageBodyWriter = serviceProvider.GetRequiredService<IncomingMessageBodyWriter>();
37+
endpointConfiguration.RegisterMessageMutator(incomingMessageBodyWriter);
38+
#endregion
39+
40+
endpointConfiguration.UseTransport(new LearningTransport());
41+
builder.UseNServiceBus(endpointConfiguration);
42+
43+
var host = builder.Build();
44+
await host.StartAsync();
45+
46+
Console.WriteLine("Press any key to exit");
47+
Console.ReadKey();
48+
49+
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+
<ProjectReference Include="..\Shared\Shared.csproj" />
9+
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.*" />
10+
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
11+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="5.0.0-alpha.2" />
12+
</ItemGroup>
13+
</Project>

0 commit comments

Comments
 (0)