diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/CustomizeRouting.sln b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/CustomizeRouting.sln new file mode 100644 index 00000000000..f7b46c85735 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/CustomizeRouting.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}") = "Endpoint2", "Endpoint2\Endpoint2.csproj", "{2FE71442-7F81-428E-B945-D564850D6564}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Endpoint1", "Endpoint1\Endpoint1.csproj", "{11641841-C7E9-4B49-9688-99E54187A7E8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{DD438DB2-9C03-4BC0-BA52-BB7A35098458}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.Build.0 = Debug|Any CPU + {11641841-C7E9-4B49-9688-99E54187A7E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {11641841-C7E9-4B49-9688-99E54187A7E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.Build.0 = Debug|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Endpoint1.csproj b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Endpoint1.csproj new file mode 100644 index 00000000000..f8ca1fca952 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Endpoint1.csproj @@ -0,0 +1,13 @@ + + + net9.0 + Exe + 13.0 + + + + + + + + \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Message2Handler.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Message2Handler.cs new file mode 100644 index 00000000000..2d710e69a9a --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Message2Handler.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using NServiceBus; +using NServiceBus.Logging; + +public class Message2Handler : + IHandleMessages +{ + static ILog log = LogManager.GetLogger(); + + public Task Handle(Message2 message, IMessageHandlerContext context) + { + log.Info($"Received Message2: {message.Property}"); + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Program.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Program.cs new file mode 100644 index 00000000000..4ca93d3f581 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint1/Program.cs @@ -0,0 +1,58 @@ +using System; +using System.Threading.Tasks; +using NServiceBus; + +class Program +{ + static async Task Main() + { + Console.Title = "Endpoint1"; + + #region config + + var endpointConfiguration = new EndpointConfiguration("Samples.ASBS.SendReply.Endpoint1"); + endpointConfiguration.EnableInstallers(); + + var connectionString = Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString"); + if (string.IsNullOrWhiteSpace(connectionString)) + { + throw new Exception("Could not read the 'AzureServiceBus_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var transport = new AzureServiceBusTransport(connectionString, TopicTopology.Default); + endpointConfiguration.UseTransport(transport); + endpointConfiguration.UseSerialization(); + + endpointConfiguration.Pipeline.Register(new PublishAllMessagesBehavior(), "Overrides message delivery mode"); + endpointConfiguration.Pipeline.Register(new PublishSendsBehavior(), "Overrides sends delivery mode"); + endpointConfiguration.Pipeline.Register(new PublishRepliesBehavior(), "Overrides replies delivery mode"); + + #endregion + + var endpointInstance = await Endpoint.Start(endpointConfiguration); + + await endpointInstance.Subscribe(); + + Console.WriteLine("Press 'enter' to send a message"); + Console.WriteLine("Press any other key to exit"); + + while (true) + { + var key = Console.ReadKey(); + Console.WriteLine(); + + if (key.Key != ConsoleKey.Enter) + { + break; + } + + var message = new Message1 + { + Property = "Hello from Endpoint1" + }; + await endpointInstance.Send("Samples.ASBS.SendReply.Endpoint2", message); + Console.WriteLine("Message1 sent"); + } + await endpointInstance.Stop(); + } +} diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Endpoint2.csproj b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Endpoint2.csproj new file mode 100644 index 00000000000..f8ca1fca952 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Endpoint2.csproj @@ -0,0 +1,13 @@ + + + net9.0 + Exe + 13.0 + + + + + + + + \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Message1Handler.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Message1Handler.cs new file mode 100644 index 00000000000..578ca48b62c --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Message1Handler.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using NServiceBus; +using NServiceBus.Logging; + +public class Message1Handler : + IHandleMessages +{ + static ILog log = LogManager.GetLogger(); + + public Task Handle(Message1 message, IMessageHandlerContext context) + { + log.Info($"Received Message1: {message.Property}"); + + var message2 = new Message2 + { + Property = "Hello from Endpoint2" + }; + return context.Reply(message2); + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Program.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Program.cs new file mode 100644 index 00000000000..28e633308cf --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Endpoint2/Program.cs @@ -0,0 +1,38 @@ +using System; +using System.Threading.Tasks; +using NServiceBus; + +class Program +{ + static async Task Main() + { + Console.Title = "Endpoint2"; + + var endpointConfiguration = new EndpointConfiguration("Samples.ASBS.SendReply.Endpoint2"); + endpointConfiguration.EnableInstallers(); + + + var connectionString = Environment.GetEnvironmentVariable("AzureServiceBus_ConnectionString"); + if (string.IsNullOrWhiteSpace(connectionString)) + { + throw new Exception("Could not read the 'AzureServiceBus_ConnectionString' environment variable. Check the sample prerequisites."); + } + + var transport = new AzureServiceBusTransport(connectionString, TopicTopology.Default); + endpointConfiguration.UseTransport(transport); + endpointConfiguration.UseSerialization(); + + endpointConfiguration.Pipeline.Register(new PublishAllMessagesBehavior(), "Overrides message delivery mode"); + endpointConfiguration.Pipeline.Register(new PublishSendsBehavior(), "Overrides sends delivery mode"); + endpointConfiguration.Pipeline.Register(new PublishRepliesBehavior(), "Overrides replies delivery mode"); + + var endpointInstance = await Endpoint.Start(endpointConfiguration); + + await endpointInstance.Subscribe(); + + Console.WriteLine("Press any key to exit"); + Console.ReadKey(); + + await endpointInstance.Stop(); + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message1.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message1.cs new file mode 100644 index 00000000000..764282cc0be --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message1.cs @@ -0,0 +1,7 @@ +using NServiceBus; + +public class Message1 : + IMessage +{ + public string Property { get; set; } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message2.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message2.cs new file mode 100644 index 00000000000..a7c3c836b18 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Message2.cs @@ -0,0 +1,7 @@ +using NServiceBus; + +public class Message2 : + IMessage +{ + public string Property { get; set; } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishAllMessagesBehavior.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishAllMessagesBehavior.cs new file mode 100644 index 00000000000..e120693ef93 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishAllMessagesBehavior.cs @@ -0,0 +1,26 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using NServiceBus.Pipeline; +using NServiceBus.Routing; + +public class PublishAllMessagesBehavior : Behavior +{ + public override Task Invoke(IRoutingContext context, Func next) + { + if (context.Extensions.TryGet(out State _)) + { + //Override the routing strategies if the message comes from the user code + var logicalMessage = context.Extensions.Get(); + var newRoutingStrategies = + context.RoutingStrategies.Select(x => new MulticastRoutingStrategy(logicalMessage.MessageType)); + context.RoutingStrategies = newRoutingStrategies.ToList(); + } + + return next(); + } + + public class State + { + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishRepliesBehavior.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishRepliesBehavior.cs new file mode 100644 index 00000000000..155845d884a --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishRepliesBehavior.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; +using NServiceBus.Pipeline; + +public class PublishRepliesBehavior : Behavior +{ + public override Task Invoke(IOutgoingReplyContext context, Func next) + { + context.Extensions.Set(new PublishAllMessagesBehavior.State()); + return next(); + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishSendsBehavior.cs b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishSendsBehavior.cs new file mode 100644 index 00000000000..ffff7617fb4 --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/PublishSendsBehavior.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; +using NServiceBus.Pipeline; + +public class PublishSendsBehavior : Behavior +{ + public override Task Invoke(IOutgoingSendContext context, Func next) + { + context.Extensions.Set(new PublishAllMessagesBehavior.State()); + return next(); + } +} \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Shared.csproj b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Shared.csproj new file mode 100644 index 00000000000..3ad66f72f0e --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/ASBS_5/Shared/Shared.csproj @@ -0,0 +1,9 @@ + + + net9.0 + 13.0 + + + + + \ No newline at end of file diff --git a/samples/azure-service-bus-netstandard/customize-routing/sample.md b/samples/azure-service-bus-netstandard/customize-routing/sample.md new file mode 100644 index 00000000000..a1019fa5c0b --- /dev/null +++ b/samples/azure-service-bus-netstandard/customize-routing/sample.md @@ -0,0 +1,35 @@ +--- +title: Azure Service Bus Send/Reply Sample +summary: Demonstrates the send/reply pattern with Azure Service Bus +reviewed: 2023-05-14 +component: ASBS +related: +- transports/azure-service-bus +--- + + +## Prerequisites + +include: asb-connectionstring-xplat + + +## Code walk-through + +This sample shows a basic two-endpoint scenario exchanging messages with each other so that: + + * `Endpoint1` sends a `Message1` message to `Endpoint2`. + * `Endpoint2` replies to `Endpoint1` with a `Message2` instance. + + +### Transport configuration + +snippet: config + + +## Viewing messages in-flight + +The following queues for the two endpoints can be seen in the Azure Portal or a third-party tool: + + * `samples.asbs.sendreply.endpoint1` + * `samples.asbs.sendreply.endpoint2` + * `error`