Skip to content

Commit 49d77af

Browse files
authored
Update databus custom-serializer to NServiceBus 10 (#7514)
1 parent 8d138e6 commit 49d77af

18 files changed

+242
-209
lines changed

samples/databus/custom-serializer/Core_9/Sender/InputLoopService.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

samples/databus/custom-serializer/Core_9/Sender/Program.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
using NServiceBus;
22
using Shared;
33
using System;
4-
using System.Threading.Tasks;
54
using NServiceBus.ClaimCheck;
65
using Microsoft.Extensions.Hosting;
76

8-
97
Console.Title = "Receiver";
108
var builder = Host.CreateApplicationBuilder(args);
11-
129
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Receiver");
1310

1411
#region ConfigureReceiverCustomDataBusSerializer
1512

1613
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, BsonClaimCheckSerializer>();
17-
claimCheck.BasePath(@"..\..\..\..\storage");
14+
claimCheck.BasePath(SolutionDirectoryFinder.Find("storage"));
1815

1916
#endregion
2017

2118
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
2219
endpointConfiguration.UseTransport(new LearningTransport());
2320

24-
Console.WriteLine("Press any key, the application is starting");
25-
Console.ReadKey();
26-
Console.WriteLine("Starting...");
27-
2821
builder.UseNServiceBus(endpointConfiguration);
2922

30-
await builder.Build().RunAsync();
23+
var host = builder.Build();
24+
await host.StartAsync();
25+
26+
Console.WriteLine("Press any key to exit");
27+
Console.ReadKey();
28+
29+
await host.StopAsync();

samples/databus/custom-serializer/DataBus_1/Sender/InputLoopService.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

samples/databus/custom-serializer/DataBus_1/Sender/Program.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,73 @@
22
using Shared;
33
using System;
44
using Microsoft.Extensions.Hosting;
5-
using Sender;
65
using Microsoft.Extensions.DependencyInjection;
7-
6+
using System.Threading.Tasks;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using NServiceBus.ClaimCheck;
810

911
Console.Title = "Sender";
1012
var builder = Host.CreateApplicationBuilder(args);
11-
builder.Services.AddHostedService<InputLoopService>();
1213
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Sender");
1314

1415
#region ConfigureSenderCustomDataBusSerializer
1516

1617
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, BsonClaimCheckSerializer>();
17-
claimCheck.BasePath(@"..\..\..\..\storage");
18+
claimCheck.BasePath(SolutionDirectoryFinder.Find("storage"));
1819

1920
#endregion
2021

2122
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
23+
endpointConfiguration.UseTransport(new LearningTransport());
2224
builder.UseNServiceBus(endpointConfiguration);
2325

24-
await builder.Build().RunAsync();
26+
var app = builder.Build();
27+
28+
await app.StartAsync();
29+
30+
var messageSession = app.Services.GetRequiredService<IMessageSession>();
31+
32+
Console.WriteLine("Press Enter to send a large message with claim check");
33+
Console.WriteLine("Press any other key to exit");
34+
35+
while (true)
36+
{
37+
var key = Console.ReadKey(true);
38+
Console.WriteLine();
39+
40+
if (key.Key == ConsoleKey.Enter)
41+
{
42+
await SendMessageLargePayload(messageSession);
43+
continue;
44+
}
45+
break;
46+
}
47+
48+
await app.StopAsync();
49+
50+
static Task SendMessageLargePayload(IMessageSession messageSession)
51+
{
52+
var measurements = GetMeasurements().ToArray();
53+
54+
var message = new MessageWithLargePayload
55+
{
56+
SomeProperty = "This message contains a large collection that will be sent on the claim check",
57+
LargeData = new ClaimCheckProperty<Measurement[]>(measurements)
58+
};
59+
Console.WriteLine($"Message sent, the payload is stored in: {SolutionDirectoryFinder.Find("storage")}");
60+
return messageSession.Send("Samples.DataBus.Receiver", message);
61+
}
62+
63+
static IEnumerable<Measurement> GetMeasurements()
64+
{
65+
for (var i = 0; i < 10000; i++)
66+
{
67+
yield return new Measurement
68+
{
69+
Timestamp = DateTimeOffset.UtcNow,
70+
MeasurementName = $"Instrument {i}",
71+
MeasurementValue = i * 10m
72+
};
73+
}
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Shared;
5+
6+
public static class SolutionDirectoryFinder
7+
{
8+
public static string Find(string solutionRelativePath = null)
9+
{
10+
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
11+
while (directory != null && !ContainsSolutionFile(directory))
12+
{
13+
directory = directory.Parent;
14+
}
15+
16+
if (directory == null)
17+
{
18+
throw new Exception("Could not find solution directory");
19+
}
20+
21+
if (string.IsNullOrEmpty(solutionRelativePath))
22+
{
23+
return directory.FullName;
24+
}
25+
26+
return Path.Combine(directory.FullName, solutionRelativePath);
27+
}
28+
29+
static bool ContainsSolutionFile(DirectoryInfo directory)
30+
{
31+
return directory.GetFiles("*.sln").Length > 0;
32+
}
33+
}

samples/databus/custom-serializer/Core_9/Receiver/MessageWithLargePayloadHandler.cs renamed to samples/databus/custom-serializer/DataBus_2/Receiver/MessageWithLargePayloadHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public class MessageWithLargePayloadHandler(ILogger<MessageWithLargePayloadHandl
99

1010
public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context)
1111
{
12-
#pragma warning disable CS0618 // Type or member is obsolete
1312
logger.LogInformation("Message received containing {MeasurementCount} measurements", message.LargeData.Value.Length);
14-
#pragma warning restore CS0618 // Type or member is obsolete
1513
return Task.CompletedTask;
1614
}
1715
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1+
using System;
12
using Microsoft.Extensions.Hosting;
23
using NServiceBus;
4+
using NServiceBus.ClaimCheck;
35
using Shared;
4-
using System;
5-
using System.Threading.Tasks;
66

77
Console.Title = "Receiver";
88
var builder = Host.CreateApplicationBuilder(args);
99
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Receiver");
1010

11-
#pragma warning disable CS0618 // Type or member is obsolete
1211
#region ConfigureReceiverCustomDataBusSerializer
1312

14-
var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus, BsonDataBusSerializer>();
15-
dataBus.BasePath(@"..\..\..\..\storage");
13+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, BsonClaimCheckSerializer>();
14+
claimCheck.BasePath(SolutionDirectoryFinder.Find("storage"));
1615

1716
#endregion
18-
#pragma warning restore CS0618 // Type or member is obsolete
1917

2018
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
2119
endpointConfiguration.UseTransport(new LearningTransport());
2220

23-
Console.ReadKey();
2421
builder.UseNServiceBus(endpointConfiguration);
2522

26-
await builder.Build().RunAsync();
23+
var host = builder.Build();
24+
await host.StartAsync();
25+
26+
Console.WriteLine("Press any key to exit");
27+
Console.ReadKey();
28+
29+
await host.StopAsync();
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
3+
<TargetFramework>net10.0</TargetFramework>
44
<OutputType>Exe</OutputType>
5-
<LangVersion>12.0</LangVersion>
5+
<LangVersion>preview</LangVersion>
66
</PropertyGroup>
77
<ItemGroup>
8+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
9+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
811
<ProjectReference Include="..\Shared\Shared.csproj" />
9-
<PackageReference Include="NServiceBus" Version="9.*" />
10-
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.0.1" />
1112
</ItemGroup>
1213
</Project>

0 commit comments

Comments
 (0)