From 7e362174ea4eddd4af784d6e17e6f94f25a84b5f Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 12 Oct 2023 17:02:05 -0400 Subject: [PATCH 1/6] Remove leftover serialization methods --- .../DataBus/DataBusProperty.cs | 32 +------------------ 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 08b00cc7dd..782a7e7093 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -1,8 +1,6 @@ namespace NServiceBus { using System; - using System.Runtime.Serialization; - using System.Security; using System.Text.Json.Serialization; /// @@ -33,23 +31,10 @@ public DataBusProperty(T value) SetValue(value); } - /// - /// For serialization purposes. - /// - /// The to populate with data. - /// The destination (see ) for this serialization. - /// The caller does not have the required permission. - protected DataBusProperty(SerializationInfo info, StreamingContext context) - { - ArgumentNullException.ThrowIfNull(info); - Key = info.GetString("Key"); - HasValue = info.GetBoolean("HasValue"); - } - /// /// The value. /// -#pragma warning disable IDE0032 // Use auto property - Value will be serialized into the message body if it is an auto property +#pragma warning disable IDE0032 // Use auto property - Value will be serialized into the message body if it is an auto property [JsonIgnore] public T Value => value; #pragma warning restore IDE0032 // Use auto property @@ -89,23 +74,8 @@ public object GetValue() return Value; } - - /// - /// Populates a with the data needed to serialize the target object. - /// - /// The to populate with data. - /// The destination (see ) for this serialization. - /// The caller does not have the required permission. - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - ArgumentNullException.ThrowIfNull(info); - info.AddValue("Key", Key); - info.AddValue("HasValue", HasValue); - } - #pragma warning disable IDE0032 // Use auto property - value will be serialized into the message body if it is an auto property T value; #pragma warning restore IDE0032 // Use auto property } - } \ No newline at end of file From f0c622437574677f0e4dd0552dfd038ba316e460 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 12 Oct 2023 17:06:44 -0400 Subject: [PATCH 2/6] Use property initializer for Type --- src/NServiceBus.Core/DataBus/DataBusProperty.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 782a7e7093..19868170fc 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -12,24 +12,13 @@ public class DataBusProperty : IDataBusProperty where T : class /// /// initializes a with no value set. /// - public DataBusProperty() - { - Type = typeof(T); - } + public DataBusProperty() { } /// /// initializes a with the . /// /// The value to initialize with. - public DataBusProperty(T value) - { - if (value != null) - { - Type = typeof(T); - } - - SetValue(value); - } + public DataBusProperty(T value) => SetValue(value); /// /// The value. @@ -43,7 +32,7 @@ public DataBusProperty(T value) /// The property . /// [JsonIgnore] - public Type Type { get; } + public Type Type { get; } = typeof(T); /// /// The key. From 784f3eb5cf2e7580ab4014acf816edd164314c7c Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 12 Oct 2023 17:34:07 -0400 Subject: [PATCH 3/6] Convert Value to auto-property and add XmlIgnore --- .../DataBus/DataBusProperty.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 19868170fc..b56f9481a9 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -2,6 +2,7 @@ { using System; using System.Text.Json.Serialization; + using System.Xml.Serialization; /// /// Default implementation for . @@ -23,10 +24,9 @@ public DataBusProperty() { } /// /// The value. /// -#pragma warning disable IDE0032 // Use auto property - Value will be serialized into the message body if it is an auto property [JsonIgnore] - public T Value => value; -#pragma warning restore IDE0032 // Use auto property + [XmlIgnore] + public T Value { get; private set; } /// /// The property . @@ -50,21 +50,14 @@ public DataBusProperty() { } /// The value to set. public void SetValue(object valueToSet) { - value = valueToSet as T; - HasValue = value != null; + Value = valueToSet as T; + HasValue = Value != null; } /// /// Gets the value of the . /// /// The value. - public object GetValue() - { - return Value; - } - -#pragma warning disable IDE0032 // Use auto property - value will be serialized into the message body if it is an auto property - T value; -#pragma warning restore IDE0032 // Use auto property + public object GetValue() => Value; } } \ No newline at end of file From 8c5c1f7966ccc2f1469c58390f12c77035051261 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 12 Oct 2023 17:38:47 -0400 Subject: [PATCH 4/6] Fix typos --- src/NServiceBus.Core/DataBus/DataBusProperty.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index b56f9481a9..a3a1f7524d 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -11,12 +11,12 @@ public class DataBusProperty : IDataBusProperty where T : class { /// - /// initializes a with no value set. + /// Initializes a with no value set. /// public DataBusProperty() { } /// - /// initializes a with the . + /// Initializes a with the . /// /// The value to initialize with. public DataBusProperty(T value) => SetValue(value); From 4f81bbf2efd03335a3aed97499f727446e75f7f7 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 12 Oct 2023 19:28:20 -0400 Subject: [PATCH 5/6] Update API approval --- .../ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt index 403d5d0380..8c4104974e 100644 --- a/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt +++ b/src/NServiceBus.Core.Tests/ApprovalFiles/APIApprovals.ApproveNServiceBus.approved.txt @@ -164,14 +164,13 @@ namespace NServiceBus { public DataBusProperty() { } public DataBusProperty(T value) { } - protected DataBusProperty(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public bool HasValue { get; set; } public string Key { get; set; } [System.Text.Json.Serialization.JsonIgnore] public System.Type Type { get; } [System.Text.Json.Serialization.JsonIgnore] + [System.Xml.Serialization.XmlIgnore] public T Value { get; } - public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public object GetValue() { } public void SetValue(object valueToSet) { } } From f2eb9d96b5a90d0db7fe8c1fe09b8d0dbf8b81ac Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Fri, 13 Oct 2023 15:04:16 -0400 Subject: [PATCH 6/6] Update DataBus tests to cover both message serializers --- ...ies_with_systemjson_message_serializer.cs} | 18 +-- ...properties_with_xml_message_serializer.cs} | 5 +- ...ties_with_systemjson_message_serializer.cs | 108 ++++++++++++++++++ ...properties_with_xml_message_serializer.cs} | 19 +-- 4 files changed, 130 insertions(+), 20 deletions(-) rename src/NServiceBus.AcceptanceTests/DataBus/{When_sending_databus_properties_with_systemjsonserializer.cs => When_sending_databus_properties_with_systemjson_message_serializer.cs} (86%) rename src/NServiceBus.AcceptanceTests/DataBus/{When_sending_databus_properties.cs => When_sending_databus_properties_with_xml_message_serializer.cs} (93%) create mode 100644 src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_systemjson_message_serializer.cs rename src/NServiceBus.AcceptanceTests/DataBus/{When_sending_databus_properties_with_unobtrusive.cs => When_sending_unobtrusive_databus_properties_with_xml_message_serializer.cs} (84%) diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjson_message_serializer.cs similarity index 86% rename from src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs rename to src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjson_message_serializer.cs index d2b877f372..eb636e3ea5 100644 --- a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjsonserializer.cs +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_systemjson_message_serializer.cs @@ -6,10 +6,10 @@ using AcceptanceTesting; using AcceptanceTesting.Customization; using EndpointTemplates; - using NServiceBus.MessageMutator; + using MessageMutator; using NUnit.Framework; - public class When_sending_databus_properties_with_systemjsonserializer : NServiceBusAcceptanceTest + public class When_sending_databus_properties_with_systemjson_message_serializer : NServiceBusAcceptanceTest { [Test] public async Task Should_receive_messages_with_largepayload_correctly() @@ -78,17 +78,17 @@ public Task Handle(MyMessageWithLargePayload messageWithLargePayload, IMessageHa Context testContext; } - } - public class Mutator : IMutateIncomingTransportMessages - { - public Task MutateIncoming(MutateIncomingTransportMessageContext context) + public class Mutator : IMutateIncomingTransportMessages { - if (context.Body.Length > PayloadSize) + public Task MutateIncoming(MutateIncomingTransportMessageContext context) { - throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + if (context.Body.Length > PayloadSize) + { + throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + } + return Task.CompletedTask; } - return Task.CompletedTask; } } diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_xml_message_serializer.cs similarity index 93% rename from src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties.cs rename to src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_xml_message_serializer.cs index fa6e73058d..b1e741c8d6 100644 --- a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties.cs +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_xml_message_serializer.cs @@ -9,7 +9,7 @@ using MessageMutator; using NUnit.Framework; - public class When_sending_databus_properties : NServiceBusAcceptanceTest + public class When_sending_databus_properties_with_xml_message_serializer : NServiceBusAcceptanceTest { [Test] public async Task Should_receive_messages_with_largepayload_correctly() @@ -43,7 +43,7 @@ public Sender() { var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); builder.UseDataBus().BasePath(basePath); - + builder.UseSerialization(); builder.ConfigureRouting().RouteToEndpoint(typeof(MyMessageWithLargePayload), typeof(Receiver)); }); } @@ -57,6 +57,7 @@ public Receiver() { var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); builder.RegisterMessageMutator(new Mutator()); }); } diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_systemjson_message_serializer.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_systemjson_message_serializer.cs new file mode 100644 index 0000000000..0f3c4d47f8 --- /dev/null +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_systemjson_message_serializer.cs @@ -0,0 +1,108 @@ +namespace NServiceBus.AcceptanceTests.DataBus +{ + using System; + using System.IO; + using System.Threading.Tasks; + using AcceptanceTesting; + using AcceptanceTesting.Customization; + using EndpointTemplates; + using MessageMutator; + using NUnit.Framework; + + public class When_sending_unobtrusive_databus_properties_with_systemjson_message_serializer : NServiceBusAcceptanceTest + { + [Test] + public async Task Should_receive_messages_with_largepayload_correctly() + { + var payloadToSend = new byte[PayloadSize]; + + var context = await Scenario.Define() + .WithEndpoint(b => b.When(session => session.Send(new MyMessageWithLargePayload + { + Payload = payloadToSend + }))) + .WithEndpoint() + .Done(c => c.ReceivedPayload != null) + .Run(); + + Assert.AreEqual(payloadToSend, context.ReceivedPayload, "The large payload should be marshalled correctly using the databus"); + } + + const int PayloadSize = 500; + + public class Context : ScenarioContext + { + public byte[] ReceivedPayload { get; set; } + } + + public class Sender : EndpointConfigurationBuilder + { + public Sender() + { + EndpointSetup(builder => + { + builder.Conventions() + .DefiningCommandsAs(t => t.Namespace != null && t.FullName == typeof(MyMessageWithLargePayload).FullName) + .DefiningDataBusPropertiesAs(t => t.Name.Contains("Payload")); + + var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); + builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); + builder.ConfigureRouting().RouteToEndpoint(typeof(MyMessageWithLargePayload), typeof(Receiver)); + }).ExcludeType(); // remove that type from assembly scanning to simulate what would happen with true unobtrusive mode + } + } + + public class Receiver : EndpointConfigurationBuilder + { + public Receiver() + { + EndpointSetup(builder => + { + builder.Conventions() + .DefiningCommandsAs(t => t.Namespace != null && t.FullName == typeof(MyMessageWithLargePayload).FullName) + .DefiningDataBusPropertiesAs(t => t.Name.Contains("Payload")); + + var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); + builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); + builder.RegisterMessageMutator(new Mutator()); + }); + } + + public class MyMessageHandler : IHandleMessages + { + public MyMessageHandler(Context context) + { + testContext = context; + } + + public Task Handle(MyMessageWithLargePayload messageWithLargePayload, IMessageHandlerContext context) + { + testContext.ReceivedPayload = messageWithLargePayload.Payload; + + return Task.CompletedTask; + } + + Context testContext; + } + + public class Mutator : IMutateIncomingTransportMessages + { + public Task MutateIncoming(MutateIncomingTransportMessageContext context) + { + if (context.Body.Length > PayloadSize) + { + throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + } + return Task.CompletedTask; + } + } + } + + public class MyMessageWithLargePayload + { + public byte[] Payload { get; set; } + } + } +} \ No newline at end of file diff --git a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_unobtrusive.cs b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_xml_message_serializer.cs similarity index 84% rename from src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_unobtrusive.cs rename to src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_xml_message_serializer.cs index 10f89cd812..d3453b0862 100644 --- a/src/NServiceBus.AcceptanceTests/DataBus/When_sending_databus_properties_with_unobtrusive.cs +++ b/src/NServiceBus.AcceptanceTests/DataBus/When_sending_unobtrusive_databus_properties_with_xml_message_serializer.cs @@ -9,7 +9,7 @@ using MessageMutator; using NUnit.Framework; - public class When_sending_databus_properties_with_unobtrusive : NServiceBusAcceptanceTest + public class When_sending_unobtrusive_databus_properties_with_xml_message_serializer : NServiceBusAcceptanceTest { [Test] public async Task Should_receive_messages_with_largepayload_correctly() @@ -47,7 +47,7 @@ public Sender() var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); builder.UseDataBus().BasePath(basePath); - + builder.UseSerialization(); builder.ConfigureRouting().RouteToEndpoint(typeof(MyMessageWithLargePayload), typeof(Receiver)); }).ExcludeType(); // remove that type from assembly scanning to simulate what would happen with true unobtrusive mode } @@ -65,6 +65,7 @@ public Receiver() var basePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "databus", "sender"); builder.UseDataBus().BasePath(basePath); + builder.UseSerialization(); builder.RegisterMessageMutator(new Mutator()); }); } @@ -85,17 +86,17 @@ public Task Handle(MyMessageWithLargePayload messageWithLargePayload, IMessageHa Context testContext; } - } - public class Mutator : IMutateIncomingTransportMessages - { - public Task MutateIncoming(MutateIncomingTransportMessageContext context) + public class Mutator : IMutateIncomingTransportMessages { - if (context.Body.Length > PayloadSize) + public Task MutateIncoming(MutateIncomingTransportMessageContext context) { - throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + if (context.Body.Length > PayloadSize) + { + throw new Exception("The message body is too large, which means the DataBus was not used to transfer the payload."); + } + return Task.CompletedTask; } - return Task.CompletedTask; } }