diff --git a/docs/tutorials/interop.md b/docs/tutorials/interop.md index 99944a5c2..64caee862 100644 --- a/docs/tutorials/interop.md +++ b/docs/tutorials/interop.md @@ -187,6 +187,10 @@ builder.UseWolverine(opts => mapper.MapProperty(x => x.ReplyUri!, (e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"), (e, msg) => msg.ReplyTo = "response"); + + // customize the incoming mapping + mapper.MapIncomingProperty(x => x.ReplyUri!, + (e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}")); }); diff --git a/src/Testing/CoreTests/Transports/MapIncomingPropertyTests.cs b/src/Testing/CoreTests/Transports/MapIncomingPropertyTests.cs new file mode 100644 index 000000000..8c282672c --- /dev/null +++ b/src/Testing/CoreTests/Transports/MapIncomingPropertyTests.cs @@ -0,0 +1,62 @@ +using Wolverine.Configuration; +using Wolverine.Runtime; +using Wolverine.Transports; +using Wolverine.Transports.Sending; +using Xunit; + +namespace CoreTests.Transports; + +public class MapIncomingPropertyTests +{ + [Fact] + public void uses_the_custom_incoming_mapping() + { + var expectedReplyUri = new Uri("stub://test:123/"); + var mapper = new StubEnvelopeMapper(new StubEndpoint()); + mapper.MapIncomingProperty(x => x.ReplyUri!, (envelope, incoming) => + { + envelope.ReplyUri = new Uri(incoming.SpecialReply!); + }); + + var envelope = new Envelope(); + mapper.MapIncomingToEnvelope(envelope, new StubTransportMessage + { + SpecialReply = expectedReplyUri.ToString() + }); + + envelope.ReplyUri.ShouldBe(expectedReplyUri); + } +} + +internal class StubTransportMessage +{ + public Dictionary Headers { get; } = new(); + public string? SpecialReply { get; set; } +} + +internal class StubEnvelopeMapper(Endpoint endpoint) + : EnvelopeMapper(endpoint) +{ + protected override void writeOutgoingHeader(StubTransportMessage outgoing, string key, string value) + { + outgoing.Headers[key] = value; + } + + protected override bool tryReadIncomingHeader(StubTransportMessage incoming, string key, out string? value) + { + return incoming.Headers.TryGetValue(key, out value); + } +} + +internal class StubEndpoint() : Endpoint(new Uri("stub://mapper"), EndpointRole.Application) +{ + public override ValueTask BuildListenerAsync(IWolverineRuntime runtime, IReceiver receiver) + { + throw new NotImplementedException(); + } + + protected override ISender CreateSender(IWolverineRuntime runtime) + { + throw new NotImplementedException(); + } +} diff --git a/src/Transports/Azure/Wolverine.AzureServiceBus.Tests/DocumentationSamples.cs b/src/Transports/Azure/Wolverine.AzureServiceBus.Tests/DocumentationSamples.cs index 597e88125..b2b5aaa28 100644 --- a/src/Transports/Azure/Wolverine.AzureServiceBus.Tests/DocumentationSamples.cs +++ b/src/Transports/Azure/Wolverine.AzureServiceBus.Tests/DocumentationSamples.cs @@ -477,6 +477,10 @@ public static async Task custom_mapping() mapper.MapProperty(x => x.ReplyUri!, (e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}"), (e, msg) => msg.ReplyTo = "response"); + + // customize the incoming mapping + mapper.MapIncomingProperty(x => x.ReplyUri!, + (e, msg) => e.ReplyUri = new Uri($"asb://queue/{msg.ReplyTo}")); }); @@ -520,4 +524,4 @@ public static async Task nservicebus() } } -public interface IInterfaceMessage; \ No newline at end of file +public interface IInterfaceMessage; diff --git a/src/Wolverine/Transports/EnvelopeMapper.cs b/src/Wolverine/Transports/EnvelopeMapper.cs index 0cab18a4d..ecce8980c 100644 --- a/src/Wolverine/Transports/EnvelopeMapper.cs +++ b/src/Wolverine/Transports/EnvelopeMapper.cs @@ -148,6 +148,13 @@ public void MapProperty(Expression> property, Action> property, + Action readFromIncoming) + { + var prop = ReflectionHelper.GetProperty(property); + _incomingToEnvelope[prop] = readFromIncoming; + } + public void MapOutgoingProperty(Expression> property, Action writeToOutgoing) {