Skip to content

Commit 4407238

Browse files
authored
Update databus-custom-serializer-converter to NServiceBus 10 (#7515)
* Add baseline copy from DataBus_1 to DataBus_2 * Add NSB10 version
1 parent 2fb1572 commit 4407238

File tree

13 files changed

+324
-1
lines changed

13 files changed

+324
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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}") = "Sender", "Sender\Sender.csproj", "{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receiver", "Receiver\Receiver.csproj", "{6987F415-B4D5-4380-ADED-EED5AF170608}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.Logging;
3+
using NServiceBus;
4+
5+
#region MessageWithLargePayloadHandler
6+
7+
public class MessageWithLargePayloadHandler(ILogger<MessageWithLargePayloadHandler> logger) :
8+
IHandleMessages<MessageWithLargePayload>
9+
{
10+
11+
public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context)
12+
{
13+
logger.LogInformation("Message received, size of blob property: {BlobSize} Bytes", message.LargeBlob.Value.Length);
14+
return Task.CompletedTask;
15+
}
16+
}
17+
18+
#endregion
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using NServiceBus;
2+
using Shared;
3+
using System;
4+
using System.Text.Json;
5+
using Microsoft.Extensions.Hosting;
6+
7+
Console.Title = "Receiver";
8+
9+
var builder = Host.CreateApplicationBuilder(args);
10+
11+
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Receiver");
12+
13+
#region ConfigureDataBus
14+
15+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, SystemJsonClaimCheckSerializer>();
16+
claimCheck.BasePath(SolutionDirectoryFinder.Find("storage"));
17+
18+
#endregion
19+
20+
#region CustomJsonSerializerOptions
21+
var jsonSerializerOptions = new JsonSerializerOptions();
22+
jsonSerializerOptions.Converters.Add(new ClaimCheckPropertyConverterFactory());
23+
endpointConfiguration.UseSerialization<SystemJsonSerializer>().Options(jsonSerializerOptions);
24+
#endregion
25+
26+
endpointConfiguration.UseTransport<LearningTransport>();
27+
28+
builder.UseNServiceBus(endpointConfiguration);
29+
30+
var host = builder.Build();
31+
await host.StartAsync();
32+
33+
Console.WriteLine("Press any key to exit");
34+
Console.ReadKey();
35+
36+
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+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using NServiceBus;
2+
using Shared;
3+
using System;
4+
using System.Text.Json;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
Console.Title = "Sender";
10+
var builder = Host.CreateApplicationBuilder(args);
11+
12+
var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Sender");
13+
14+
#region ConfigureDataBus
15+
16+
var claimCheck = endpointConfiguration.UseClaimCheck<FileShareClaimCheck, SystemJsonClaimCheckSerializer>();
17+
claimCheck.BasePath(SolutionDirectoryFinder.Find("storage"));
18+
19+
#endregion
20+
21+
#region CustomJsonSerializerOptions
22+
var jsonSerializerOptions = new JsonSerializerOptions();
23+
jsonSerializerOptions.Converters.Add(new ClaimCheckPropertyConverterFactory());
24+
endpointConfiguration.UseSerialization<SystemJsonSerializer>().Options(jsonSerializerOptions);
25+
#endregion
26+
27+
endpointConfiguration.UseTransport<LearningTransport>();
28+
29+
builder.UseNServiceBus(endpointConfiguration);
30+
31+
var host = builder.Build();
32+
33+
await host.StartAsync();
34+
35+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
36+
37+
Console.WriteLine("Press 'D' to send a databus large message");
38+
Console.WriteLine("Press any other key to exit");
39+
40+
while (true)
41+
{
42+
var key = Console.ReadKey();
43+
Console.WriteLine();
44+
45+
if (key.Key == ConsoleKey.D)
46+
{
47+
await SendMessageLargePayload(messageSession);
48+
continue;
49+
}
50+
break;
51+
}
52+
53+
await host.StopAsync();
54+
55+
static async Task SendMessageLargePayload(IMessageSession messageSession)
56+
{
57+
#region SendMessageLargePayload
58+
59+
var message = new MessageWithLargePayload
60+
{
61+
SomeProperty = "This message contains a large blob that will be sent on the data bus",
62+
LargeBlob = new ClaimCheckProperty<byte[]>(new byte[1024 * 1024 * 5]) //5MB
63+
};
64+
await messageSession.Send("Samples.DataBus.Receiver", message);
65+
66+
#endregion
67+
68+
Console.WriteLine($@"Message sent, the payload is stored in: {SolutionDirectoryFinder.Find("storage")}");
69+
}
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+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
10+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.1" />
11+
<ProjectReference Include="..\Shared\Shared.csproj" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NServiceBus;
2+
using System;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using NServiceBus.ClaimCheck;
6+
7+
namespace Shared
8+
{
9+
#region DatabusPropertyConverter
10+
public class ClaimCheckPropertyConverter<T> : JsonConverter<ClaimCheckProperty<T>> where T : class
11+
{
12+
public ClaimCheckPropertyConverter(JsonSerializerOptions options)
13+
{
14+
15+
}
16+
public override ClaimCheckProperty<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
17+
{
18+
var sdp = JsonSerializer.Deserialize<SerializableDatabusProperty>(reader.GetString());
19+
ClaimCheckProperty<T> databusProp = new ClaimCheckProperty<T>();
20+
databusProp.HasValue = sdp.HasValue;
21+
databusProp.Key = sdp.Key;
22+
return databusProp;
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, ClaimCheckProperty<T> value, JsonSerializerOptions options)
26+
{
27+
SerializableDatabusProperty sdp = new SerializableDatabusProperty();
28+
sdp.HasValue = value.HasValue;
29+
sdp.Key = value.Key;
30+
string jsonStr = JsonSerializer.Serialize(sdp);
31+
writer.WriteStringValue(jsonStr);
32+
33+
}
34+
35+
private class SerializableDatabusProperty
36+
{
37+
public string Key { get; set; }
38+
public bool HasValue { get; set; }
39+
40+
}
41+
42+
}
43+
#endregion
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NServiceBus.ClaimCheck;
2+
using System;
3+
using System.Reflection;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
using NServiceBus;
7+
8+
namespace Shared;
9+
10+
#region DatabusPropertyConverterFactory
11+
12+
public class ClaimCheckPropertyConverterFactory : JsonConverterFactory
13+
{
14+
public override bool CanConvert(Type typeToConvert)
15+
{
16+
if (!typeToConvert.IsGenericType)
17+
{
18+
return false;
19+
}
20+
21+
if (typeToConvert.GetGenericTypeDefinition() != typeof(ClaimCheckProperty<>))
22+
{
23+
return false;
24+
}
25+
26+
return true;
27+
}
28+
29+
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
30+
{
31+
Type keyType = typeToConvert.GetGenericArguments()[0];
32+
33+
JsonConverter converter = (JsonConverter)Activator.CreateInstance(
34+
typeof(ClaimCheckPropertyConverter<>).MakeGenericType(
35+
new Type[] { keyType }),
36+
BindingFlags.Instance | BindingFlags.Public,
37+
binder: null,
38+
args: new object[] { options },
39+
culture: null)!;
40+
41+
return converter;
42+
}
43+
}
44+
45+
#endregion
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using NServiceBus;
2+
using NServiceBus.ClaimCheck;
3+
4+
#region MessageWithLargePayload
5+
6+
//the data bus is allowed to clean up transmitted properties older than the TTBR
7+
[TimeToBeReceived("00:01:00")]
8+
public class MessageWithLargePayload :
9+
ICommand
10+
{
11+
public string SomeProperty { get; set; }
12+
public ClaimCheckProperty<byte[]> LargeBlob { get; set; }
13+
}
14+
15+
#endregion
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<LangVersion>preview</LangVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="NServiceBus.ClaimCheck" Version="2.0.0-alpha.1" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)