Skip to content

Commit d19f2ef

Browse files
committed
Fixes #7389
1 parent aa0fcd5 commit d19f2ef

File tree

10 files changed

+192
-52
lines changed

10 files changed

+192
-52
lines changed
Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Sample
81
{
9-
10-
using System.Threading;
11-
using System.Threading.Tasks;
12-
using Microsoft.Extensions.Hosting;
13-
using NServiceBus;
14-
public class InputLoopService(IMessageSession messageSession) : BackgroundService
15-
{
16-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
17-
{
18-
#region message
19-
20-
var message = new CreateOrder
21-
{
22-
OrderId = 9,
23-
Date = DateTime.Now,
24-
CustomerId = 12,
25-
OrderItems = new List<OrderItem>
26-
{
27-
new OrderItem
28-
{
29-
ItemId = 6,
30-
Quantity = 2
31-
},
32-
new OrderItem
33-
{
34-
ItemId = 5,
35-
Quantity = 4
36-
},
37-
}
38-
};
39-
40-
await messageSession.SendLocal(message);
41-
Console.WriteLine("Order Sent");
42-
43-
#endregion
44-
45-
}
46-
}
47-
48-
}

samples/serializers/newtonsoft/Newtonsoft_4/Sample/Program.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
using Newtonsoft.Json;
66
using NServiceBus;
77
using NServiceBus.MessageMutator;
8-
using Sample;
9-
108

119
Console.Title = "ExternalJson";
1210

1311
var builder = Host.CreateApplicationBuilder(args);
14-
builder.Services.AddHostedService<InputLoopService>();
1512

1613
#region config
1714

@@ -38,10 +35,42 @@
3835

3936
#endregion
4037

41-
4238
Console.WriteLine("Press any key, the application is starting");
4339
Console.ReadKey();
4440
Console.WriteLine("Starting...");
4541

4642
builder.UseNServiceBus(endpointConfiguration);
47-
await builder.Build().RunAsync();
43+
var host = builder.Build();
44+
await host.StartAsync();
45+
46+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
47+
48+
#region message
49+
50+
var message = new CreateOrder
51+
{
52+
OrderId = 9,
53+
Date = DateTime.Now,
54+
CustomerId = 12,
55+
OrderItems = new List<OrderItem>
56+
{
57+
new OrderItem
58+
{
59+
ItemId = 6,
60+
Quantity = 2
61+
},
62+
new OrderItem
63+
{
64+
ItemId = 5,
65+
Quantity = 4
66+
},
67+
}
68+
};
69+
70+
await messageSession.SendLocal(message);
71+
72+
Console.WriteLine("Order Sent");
73+
74+
#endregion
75+
76+
await host.StopAsync();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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}") = "Sample", "Sample\Sample.csproj", "{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(SolutionProperties) = preSolution
17+
HideSolutionNode = FALSE
18+
EndGlobalSection
19+
EndGlobal
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NServiceBus;
4+
5+
public class CreateOrder :
6+
IMessage
7+
{
8+
public int OrderId { get; set; }
9+
public DateTime Date { get; set; }
10+
public int CustomerId { get; set; }
11+
public List<OrderItem> OrderItems { get; set; }
12+
}
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");
10+
return Task.CompletedTask;
11+
}
12+
}
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.MessageMutator;
5+
6+
#region mutator
7+
public class MessageBodyWriter(ILogger<MessageBodyWriter> logger) :
8+
IMutateIncomingTransportMessages
9+
{
10+
public Task MutateIncoming(MutateIncomingTransportMessageContext context)
11+
{
12+
var bodyAsString = Encoding.UTF8
13+
.GetString(context.Body.ToArray());
14+
15+
logger.LogInformation("Serialized Message Body:");
16+
logger.LogInformation(bodyAsString);
17+
18+
return Task.CompletedTask;
19+
}
20+
}
21+
#endregion
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class OrderItem
2+
{
3+
public int ItemId { get; set; }
4+
public int Quantity { get; set; }
5+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using Newtonsoft.Json;
6+
using NServiceBus;
7+
using NServiceBus.MessageMutator;
8+
9+
Console.Title = "ExternalJson";
10+
11+
var builder = Host.CreateApplicationBuilder(args);
12+
13+
#region config
14+
15+
var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.ExternalJson");
16+
17+
var settings = new JsonSerializerSettings
18+
{
19+
Formatting = Formatting.Indented
20+
};
21+
var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();
22+
serialization.Settings(settings);
23+
24+
#endregion
25+
26+
endpointConfiguration.UseTransport(new LearningTransport());
27+
28+
#region registermutator
29+
builder.Services.AddSingleton<MessageBodyWriter>();
30+
31+
// Then later get it from the service provider when needed
32+
var serviceProvider = builder.Services.BuildServiceProvider();
33+
var messageBodyWriter = serviceProvider.GetRequiredService<MessageBodyWriter>();
34+
endpointConfiguration.RegisterMessageMutator(messageBodyWriter);
35+
36+
#endregion
37+
38+
Console.WriteLine("Press any key, the application is starting");
39+
Console.ReadKey();
40+
Console.WriteLine("Starting...");
41+
42+
builder.UseNServiceBus(endpointConfiguration);
43+
var host = builder.Build();
44+
await host.StartAsync();
45+
46+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
47+
48+
#region message
49+
50+
var message = new CreateOrder
51+
{
52+
OrderId = 9,
53+
Date = DateTime.Now,
54+
CustomerId = 12,
55+
OrderItems = new List<OrderItem>
56+
{
57+
new OrderItem
58+
{
59+
ItemId = 6,
60+
Quantity = 2
61+
},
62+
new OrderItem
63+
{
64+
ItemId = 5,
65+
Quantity = 4
66+
},
67+
}
68+
};
69+
70+
await messageSession.SendLocal(message);
71+
72+
Console.WriteLine("Order Sent");
73+
74+
#endregion
75+
76+
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="Newtonsoft.Json" Version="13.*" />
9+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
<PackageReference Include="NServiceBus.Newtonsoft.Json" Version="5.0.0-alpha.1" />
12+
</ItemGroup>
13+
</Project>

samples/serializers/newtonsoft/Newtonsoft_5/Sample/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)