Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MEGRE] Spike publishing commands and replies #6994

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NServiceBus.Transport.AzureServiceBus" Version="5.0.0-alpha.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;

public class Message2Handler :
IHandleMessages<Message2>
{
static ILog log = LogManager.GetLogger<Message2Handler>();

public Task Handle(Message2 message, IMessageHandlerContext context)
{
log.Info($"Received Message2: {message.Property}");
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -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<SystemJsonSerializer>();

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<Message2>();

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NServiceBus.Transport.AzureServiceBus" Version="5.0.0-alpha.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;

public class Message1Handler :
IHandleMessages<Message1>
{
static ILog log = LogManager.GetLogger<Message1Handler>();

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);
}
}
Original file line number Diff line number Diff line change
@@ -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<SystemJsonSerializer>();

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<Message1>();

Console.WriteLine("Press any key to exit");
Console.ReadKey();

await endpointInstance.Stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using NServiceBus;

public class Message1 :
IMessage
{
public string Property { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using NServiceBus;

public class Message2 :
IMessage
{
public string Property { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NServiceBus.Pipeline;
using NServiceBus.Routing;

public class PublishAllMessagesBehavior : Behavior<IRoutingContext>
{
public override Task Invoke(IRoutingContext context, Func<Task> next)
{
if (context.Extensions.TryGet(out State _))
{
//Override the routing strategies if the message comes from the user code
var logicalMessage = context.Extensions.Get<OutgoingLogicalMessage>();
var newRoutingStrategies =
context.RoutingStrategies.Select(x => new MulticastRoutingStrategy(logicalMessage.MessageType));
context.RoutingStrategies = newRoutingStrategies.ToList();
}

return next();
}

public class State
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;
using NServiceBus.Pipeline;

public class PublishRepliesBehavior : Behavior<IOutgoingReplyContext>
{
public override Task Invoke(IOutgoingReplyContext context, Func<Task> next)
{
context.Extensions.Set(new PublishAllMessagesBehavior.State());
return next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;
using NServiceBus.Pipeline;

public class PublishSendsBehavior : Behavior<IOutgoingSendContext>
{
public override Task Invoke(IOutgoingSendContext context, Func<Task> next)
{
context.Extensions.Set(new PublishAllMessagesBehavior.State());
return next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus" Version="9.*" />
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions samples/azure-service-bus-netstandard/customize-routing/sample.md
Original file line number Diff line number Diff line change
@@ -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`
Loading