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; } } 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) { } } diff --git a/src/NServiceBus.Core/DataBus/DataBusProperty.cs b/src/NServiceBus.Core/DataBus/DataBusProperty.cs index 08b00cc7dd..a3a1f7524d 100644 --- a/src/NServiceBus.Core/DataBus/DataBusProperty.cs +++ b/src/NServiceBus.Core/DataBus/DataBusProperty.cs @@ -1,9 +1,8 @@ namespace NServiceBus { using System; - using System.Runtime.Serialization; - using System.Security; using System.Text.Json.Serialization; + using System.Xml.Serialization; /// /// Default implementation for . @@ -12,53 +11,28 @@ public class DataBusProperty : IDataBusProperty where T : class { /// - /// initializes a with no value set. + /// Initializes a with no value set. /// - public DataBusProperty() - { - Type = typeof(T); - } + public DataBusProperty() { } /// - /// initializes a with the . + /// Initializes a with the . /// /// The value to initialize with. - public DataBusProperty(T value) - { - if (value != null) - { - Type = typeof(T); - } - - 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"); - } + public DataBusProperty(T value) => SetValue(value); /// /// 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 . /// [JsonIgnore] - public Type Type { get; } + public Type Type { get; } = typeof(T); /// /// The key. @@ -76,36 +50,14 @@ protected DataBusProperty(SerializationInfo info, StreamingContext context) /// 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; - } - - - /// - /// 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 + public object GetValue() => Value; } - } \ No newline at end of file