Skip to content

Commit 5ae337f

Browse files
committed
Migrate databus custom-serializer to NServiceBus 10
- Updated all .csproj files to target .NET 10 and NServiceBus 10.0.0-alpha.1 - Added NServiceBus.ClaimCheck 2.0.0-alpha.1 package - Migrated from DataBusProperty<T> to regular array properties - Updated BsonDataBusSerializer to BsonClaimCheckSerializer implementing IClaimCheckSerializer - Replaced UseDataBus with UseClaimCheck<FileShareClaimCheck, BsonClaimCheckSerializer> - Added prerelease.txt marker file - Removed InputLoopService pattern and moved logic to Program.cs - Updated message handler to work with regular arrays instead of DataBusProperty<T> - Added claim check conventions for properties starting with 'Large' Fixes: #7289
1 parent 96dd5c4 commit 5ae337f

File tree

10 files changed

+95
-115
lines changed

10 files changed

+95
-115
lines changed

samples/databus/custom-serializer/Core_10/Receiver/MessageWithLargePayloadHandler.cs

Lines changed: 1 addition & 3 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
13-
logger.LogInformation("Message received containing {MeasurementCount} measurements", message.LargeData.Value.Length);
14-
#pragma warning restore CS0618 // Type or member is obsolete
12+
logger.LogInformation("Message received containing {MeasurementCount} measurements", message.LargeData.Length);
1513
return Task.CompletedTask;
1614
}
1715
}

samples/databus/custom-serializer/Core_10/Receiver/Program.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
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(@"..\..\..\..\storage");
15+
16+
#endregion
1617

18+
#region ClaimCheckConventions
19+
endpointConfiguration.Conventions().DefiningClaimCheckPropertiesAs(prop => prop.Name.StartsWith("Large"));
1720
#endregion
18-
#pragma warning restore CS0618 // Type or member is obsolete
1921

2022
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
2123
endpointConfiguration.UseTransport(new LearningTransport());
Lines changed: 5 additions & 4 deletions
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>

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

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,78 @@
1-
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.Hosting;
3-
using NServiceBus;
4-
using Sender;
5-
using Shared;
61
using System;
72
using System.Collections.Generic;
83
using System.Linq;
94
using System.Threading.Tasks;
10-
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using NServiceBus;
8+
using NServiceBus.ClaimCheck;
9+
using Shared;
1110

1211
Console.Title = "Sender";
1312
var builder = Host.CreateApplicationBuilder(args);
14-
builder.Services.AddHostedService<InputLoopService>();
1513
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Sender");
1614

17-
#pragma warning disable CS0618 // Type or member is obsolete
1815
#region ConfigureSenderCustomDataBusSerializer
1916

20-
var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus, BsonDataBusSerializer>();
21-
dataBus.BasePath(@"..\..\..\..\storage");
17+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, BsonClaimCheckSerializer>();
18+
claimCheck.BasePath(@"..\..\..\..\storage");
19+
20+
#endregion
2221

22+
#region ClaimCheckConventions
23+
endpointConfiguration.Conventions().DefiningClaimCheckPropertiesAs(prop => prop.Name.StartsWith("Large"));
2324
#endregion
24-
#pragma warning restore CS0618 // Type or member is obsolete
2525

2626
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
2727
endpointConfiguration.UseTransport(new LearningTransport());
2828
builder.UseNServiceBus(endpointConfiguration);
2929

30-
await builder.Build().RunAsync();
30+
var app = builder.Build();
31+
32+
await app.StartAsync();
33+
34+
var messageSession = app.Services.GetRequiredService<IMessageSession>();
35+
36+
Console.WriteLine("Press Enter to send a large message with claim check");
37+
Console.WriteLine("Press any other key to exit");
38+
39+
while (true)
40+
{
41+
var key = Console.ReadKey(true);
42+
Console.WriteLine();
43+
44+
if (key.Key == ConsoleKey.Enter)
45+
{
46+
await SendMessageLargePayload(messageSession);
47+
continue;
48+
}
49+
break;
50+
}
51+
52+
await app.StopAsync();
53+
54+
static Task SendMessageLargePayload(IMessageSession messageSession)
55+
{
56+
var measurements = GetMeasurements().ToArray();
57+
58+
var message = new MessageWithLargePayload
59+
{
60+
SomeProperty = "This message contains a large collection that will be sent on the claim check",
61+
LargeData = measurements
62+
};
63+
Console.WriteLine(@"Message sent, the payload is stored in: ..\..\..\storage");
64+
return messageSession.Send("Samples.DataBus.Receiver", message);
65+
}
66+
67+
static IEnumerable<Measurement> GetMeasurements()
68+
{
69+
for (var i = 0; i < 10000; i++)
70+
{
71+
yield return new Measurement
72+
{
73+
Timestamp = DateTimeOffset.UtcNow,
74+
MeasurementName = $"Instrument {i}",
75+
MeasurementValue = i * 10m
76+
};
77+
}
78+
}
Lines changed: 5 additions & 4 deletions
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>

samples/databus/custom-serializer/Core_10/Shared/BsonDataBusSerializer.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ namespace Shared
44
{
55
using Newtonsoft.Json;
66
using Newtonsoft.Json.Bson;
7-
using NServiceBus.DataBus;
7+
using NServiceBus.ClaimCheck;
88
using System;
99
using System.IO;
10-
#pragma warning disable CS0618 // Type or member is obsolete
1110

1211
#region CustomDataBusSerializer
1312

14-
public class BsonDataBusSerializer :
15-
IDataBusSerializer
13+
public class BsonClaimCheckSerializer :
14+
IClaimCheckSerializer
1615
{
1716
public void Serialize(object databusProperty, Stream stream)
1817
{
@@ -31,23 +30,18 @@ public object Deserialize(Type propertyType, Stream stream)
3130
}
3231
}
3332

34-
BinaryWriter CreateNonClosingBinaryWriter(Stream stream) =>
35-
new BinaryWriter(
36-
stream,
37-
Encoding.UTF8,
38-
leaveOpen: true);
39-
33+
static BinaryWriter CreateNonClosingBinaryWriter(Stream stream)
34+
{
35+
return new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true);
36+
}
4037

41-
JsonSerializer serializer = JsonSerializer.Create(
42-
new JsonSerializerSettings
43-
{
44-
TypeNameHandling = TypeNameHandling.All
45-
}
46-
);
38+
static JsonSerializer serializer = new JsonSerializer
39+
{
40+
TypeNameHandling = TypeNameHandling.All
41+
};
4742

4843
public string ContentType => "application/bson";
4944
}
5045

5146
#endregion
52-
#pragma warning restore CS0618 // Type or member is obsolete
5347
}

samples/databus/custom-serializer/Core_10/Shared/MessageWithLargePayload.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ public class MessageWithLargePayload :
66
ICommand
77
{
88
public string SomeProperty { get; set; }
9-
#pragma warning disable CS0618 // Type or member is obsolete
10-
public DataBusProperty<Measurement[]> LargeData { get; set; }
11-
#pragma warning restore CS0618 // Type or member is obsolete
9+
public Measurement[] LargeData { get; set; }
1210
}
1311

1412
[Serializable]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
4-
<LangVersion>12.0</LangVersion>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<LangVersion>preview</LangVersion>
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Newtonsoft.Json" Version="13.*" />
88
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.*" />
9-
<PackageReference Include="NServiceBus" Version="9.*" />
9+
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
1011
</ItemGroup>
1112
</Project>

samples/databus/custom-serializer/Core_10/prerelease.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)