diff --git a/samples/databus/custom-serializer/Core_10/DataBus.sln b/samples/databus/custom-serializer/Core_10/DataBus.sln new file mode 100644 index 00000000000..96ebc756688 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/DataBus.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29728.190 +MinimumVisualStudioVersion = 15.0.26730.12 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sender", "Sender\Sender.csproj", "{D04CF1FC-C4C0-4959-A817-2BC68770CA9B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Receiver", "Receiver\Receiver.csproj", "{6987F415-B4D5-4380-ADED-EED5AF170608}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D04CF1FC-C4C0-4959-A817-2BC68770CA9B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6987F415-B4D5-4380-ADED-EED5AF170608}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E4A4B35E-AFD3-456C-A5AC-4A88AAD77156}.Debug|Any CPU.Build.0 = Debug|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/databus/custom-serializer/Core_10/Receiver/MessageWithLargePayloadHandler.cs b/samples/databus/custom-serializer/Core_10/Receiver/MessageWithLargePayloadHandler.cs new file mode 100644 index 00000000000..6aeee556dec --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Receiver/MessageWithLargePayloadHandler.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using NServiceBus; +using NServiceBus.Logging; + +public class MessageWithLargePayloadHandler(ILogger logger) : + IHandleMessages +{ + + public Task Handle(MessageWithLargePayload message, IMessageHandlerContext context) + { + logger.LogInformation("Message received containing {MeasurementCount} measurements", message.LargeData.Length); + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Receiver/Program.cs b/samples/databus/custom-serializer/Core_10/Receiver/Program.cs new file mode 100644 index 00000000000..1ebfec7a0d1 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Receiver/Program.cs @@ -0,0 +1,28 @@ +using System; +using Microsoft.Extensions.Hosting; +using NServiceBus; +using NServiceBus.ClaimCheck; +using Shared; + +Console.Title = "Receiver"; +var builder = Host.CreateApplicationBuilder(args); +var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Receiver"); + +#region ConfigureReceiverCustomDataBusSerializer + +var claimCheck = endpointConfiguration.UseClaimCheck(); +claimCheck.BasePath(@"..\..\..\..\storage"); + +#endregion + +#region ClaimCheckConventions +endpointConfiguration.Conventions().DefiningClaimCheckPropertiesAs(prop => prop.Name.StartsWith("Large")); +#endregion + +endpointConfiguration.UseSerialization(); +endpointConfiguration.UseTransport(new LearningTransport()); + +Console.ReadKey(); +builder.UseNServiceBus(endpointConfiguration); + +await builder.Build().RunAsync(); \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Receiver/Receiver.csproj b/samples/databus/custom-serializer/Core_10/Receiver/Receiver.csproj new file mode 100644 index 00000000000..8cf0e9cc8cc --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Receiver/Receiver.csproj @@ -0,0 +1,13 @@ + + + net10.0 + Exe + preview + + + + + + + + \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Sender/Program.cs b/samples/databus/custom-serializer/Core_10/Sender/Program.cs new file mode 100644 index 00000000000..953ca66ca01 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Sender/Program.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using NServiceBus; +using NServiceBus.ClaimCheck; +using Shared; + +Console.Title = "Sender"; +var builder = Host.CreateApplicationBuilder(args); +var endpointConfiguration = new EndpointConfiguration("Samples.DataBus.Sender"); + +#region ConfigureSenderCustomDataBusSerializer + +var claimCheck = endpointConfiguration.UseClaimCheck(); +claimCheck.BasePath(@"..\..\..\..\storage"); + +#endregion + +#region ClaimCheckConventions +endpointConfiguration.Conventions().DefiningClaimCheckPropertiesAs(prop => prop.Name.StartsWith("Large")); +#endregion + +endpointConfiguration.UseSerialization(); +endpointConfiguration.UseTransport(new LearningTransport()); +builder.UseNServiceBus(endpointConfiguration); + +var app = builder.Build(); + +await app.StartAsync(); + +var messageSession = app.Services.GetRequiredService(); + +Console.WriteLine("Press Enter to send a large message with claim check"); +Console.WriteLine("Press any other key to exit"); + +while (true) +{ + var key = Console.ReadKey(true); + Console.WriteLine(); + + if (key.Key == ConsoleKey.Enter) + { + await SendMessageLargePayload(messageSession); + continue; + } + break; +} + +await app.StopAsync(); + +static Task SendMessageLargePayload(IMessageSession messageSession) +{ + var measurements = GetMeasurements().ToArray(); + + var message = new MessageWithLargePayload + { + SomeProperty = "This message contains a large collection that will be sent on the claim check", + LargeData = measurements + }; + Console.WriteLine(@"Message sent, the payload is stored in: ..\..\..\storage"); + return messageSession.Send("Samples.DataBus.Receiver", message); +} + +static IEnumerable GetMeasurements() +{ + for (var i = 0; i < 10000; i++) + { + yield return new Measurement + { + Timestamp = DateTimeOffset.UtcNow, + MeasurementName = $"Instrument {i}", + MeasurementValue = i * 10m + }; + } +} \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Sender/Sender.csproj b/samples/databus/custom-serializer/Core_10/Sender/Sender.csproj new file mode 100644 index 00000000000..8cf0e9cc8cc --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Sender/Sender.csproj @@ -0,0 +1,13 @@ + + + net10.0 + Exe + preview + + + + + + + + \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Shared/BsonDataBusSerializer.cs b/samples/databus/custom-serializer/Core_10/Shared/BsonDataBusSerializer.cs new file mode 100644 index 00000000000..a140aa53811 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Shared/BsonDataBusSerializer.cs @@ -0,0 +1,47 @@ +using System.Text; + +namespace Shared +{ + using Newtonsoft.Json; + using Newtonsoft.Json.Bson; + using NServiceBus.ClaimCheck; + using System; + using System.IO; + + #region CustomDataBusSerializer + + public class BsonClaimCheckSerializer : + IClaimCheckSerializer + { + public void Serialize(object databusProperty, Stream stream) + { + using (var writer = CreateNonClosingBinaryWriter(stream)) + using (var bsonWriter = new BsonDataWriter(writer)) + { + serializer.Serialize(bsonWriter, databusProperty); + } + } + + public object Deserialize(Type propertyType, Stream stream) + { + using (var jsonReader = new BsonDataReader(stream)) + { + return serializer.Deserialize(jsonReader, propertyType); + } + } + + static BinaryWriter CreateNonClosingBinaryWriter(Stream stream) + { + return new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true); + } + + static JsonSerializer serializer = new JsonSerializer + { + TypeNameHandling = TypeNameHandling.All + }; + + public string ContentType => "application/bson"; + } + + #endregion +} \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Shared/MessageWithLargePayload.cs b/samples/databus/custom-serializer/Core_10/Shared/MessageWithLargePayload.cs new file mode 100644 index 00000000000..57e21774702 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Shared/MessageWithLargePayload.cs @@ -0,0 +1,18 @@ +using System; +using NServiceBus; + +[TimeToBeReceived("00:01:00")] +public class MessageWithLargePayload : + ICommand +{ + public string SomeProperty { get; set; } + public Measurement[] LargeData { get; set; } +} + +[Serializable] +public class Measurement +{ + public DateTimeOffset Timestamp { get; set; } + public string MeasurementName { get; set; } + public decimal MeasurementValue { get; set; } +} \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/Shared/Shared.csproj b/samples/databus/custom-serializer/Core_10/Shared/Shared.csproj new file mode 100644 index 00000000000..bfeca15e1c6 --- /dev/null +++ b/samples/databus/custom-serializer/Core_10/Shared/Shared.csproj @@ -0,0 +1,12 @@ + + + net10.0 + preview + + + + + + + + \ No newline at end of file diff --git a/samples/databus/custom-serializer/Core_10/prerelease.txt b/samples/databus/custom-serializer/Core_10/prerelease.txt new file mode 100644 index 00000000000..e69de29bb2d