Skip to content

Commit

Permalink
Cross-version pubsub sample for ASB
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Feb 11, 2025
1 parent 765e449 commit 5563572
Show file tree
Hide file tree
Showing 20 changed files with 485 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{06CB90E0-85AD-4F8C-9D5C-D008EB63FA3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PublisherV4", "PublisherV4\PublisherV4.csproj", "{29F1787F-7118-48F6-B4A3-E6C45E23956C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PublisherV5", "PublisherV5\PublisherV5.csproj", "{38C77DC9-51F3-421D-BBE6-3DE8508AEE15}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubscriberV4", "SubscriberV4\SubscriberV4.csproj", "{E95F1759-40E5-4B72-B3AE-868CB538F5DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubscriberV5", "SubscriberV5\SubscriberV5.csproj", "{3713A7D0-E843-4FF1-9688-165F2345CFC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06CB90E0-85AD-4F8C-9D5C-D008EB63FA3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06CB90E0-85AD-4F8C-9D5C-D008EB63FA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06CB90E0-85AD-4F8C-9D5C-D008EB63FA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06CB90E0-85AD-4F8C-9D5C-D008EB63FA3A}.Release|Any CPU.Build.0 = Release|Any CPU
{29F1787F-7118-48F6-B4A3-E6C45E23956C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29F1787F-7118-48F6-B4A3-E6C45E23956C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29F1787F-7118-48F6-B4A3-E6C45E23956C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29F1787F-7118-48F6-B4A3-E6C45E23956C}.Release|Any CPU.Build.0 = Release|Any CPU
{38C77DC9-51F3-421D-BBE6-3DE8508AEE15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38C77DC9-51F3-421D-BBE6-3DE8508AEE15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38C77DC9-51F3-421D-BBE6-3DE8508AEE15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38C77DC9-51F3-421D-BBE6-3DE8508AEE15}.Release|Any CPU.Build.0 = Release|Any CPU
{E95F1759-40E5-4B72-B3AE-868CB538F5DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E95F1759-40E5-4B72-B3AE-868CB538F5DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E95F1759-40E5-4B72-B3AE-868CB538F5DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E95F1759-40E5-4B72-B3AE-868CB538F5DA}.Release|Any CPU.Build.0 = Release|Any CPU
{3713A7D0-E843-4FF1-9688-165F2345CFC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3713A7D0-E843-4FF1-9688-165F2345CFC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3713A7D0-E843-4FF1-9688-165F2345CFC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3713A7D0-E843-4FF1-9688-165F2345CFC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {071F7DAC-CB22-4DB9-86FF-4CFC7D3172B5}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;

namespace PublisherV4
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Publisher 1 running Azure Service Bus transport V4");
var builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Logging.AddConsole();

var endpointConfiguration = new EndpointConfiguration("Publisher1");

var connectionString = builder.Configuration.GetConnectionString("AzureServiceBusConnectionString");
var routing = endpointConfiguration.UseTransport(new AzureServiceBusTransport(connectionString));
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

// Operational scripting: https://docs.particular.net/transports/azure-service-bus/operational-scripting
endpointConfiguration.EnableInstallers();

builder.UseNServiceBus(endpointConfiguration);

builder.Services.AddHostedService<PublisherWorker>();

var host = builder.Build();
await host.RunAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>13.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus.Transport.AzureServiceBus" Version="4.*" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using Shared;

namespace PublisherV4
{
public class PublisherWorker : BackgroundService
{
private readonly IMessageSession messageSession;
private readonly ILogger<PublisherWorker> logger;

public PublisherWorker(IMessageSession messageSession, ILogger<PublisherWorker> logger)
{
this.messageSession = messageSession;
this.logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await messageSession.Publish(new MyEvent());

logger.LogInformation("Published MyEvent");
}
catch (OperationCanceledException)
{
// graceful shutdown
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"AzureServiceBusConnectionString": "<PLACEHOLDER>"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using Shared;

namespace PublisherV5
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Publisher 2 running Azure Service Bus transport V5");
var builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Logging.AddConsole();

var endpointConfiguration = new EndpointConfiguration("Publisher2");

var connectionString = builder.Configuration.GetConnectionString("AzureServiceBusConnectionString");
var topology = TopicTopology.MigrateFromSingleDefaultTopic();
topology.EventToMigrate<MyOtherEvent>();
var routing = endpointConfiguration.UseTransport(new AzureServiceBusTransport(connectionString, topology));
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

// Operational scripting: https://docs.particular.net/transports/azure-service-bus/operational-scripting
endpointConfiguration.EnableInstallers();

builder.UseNServiceBus(endpointConfiguration);

builder.Services.AddHostedService<PublisherWorker>();

var host = builder.Build();
await host.RunAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>13.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus.Transport.AzureServiceBus" Version="5.*-*" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using Shared;

namespace PublisherV5
{
public class PublisherWorker : BackgroundService
{
private readonly IMessageSession messageSession;
private readonly ILogger<PublisherWorker> logger;

public PublisherWorker(IMessageSession messageSession, ILogger<PublisherWorker> logger)
{
this.messageSession = messageSession;
this.logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await messageSession.Publish(new MyOtherEvent());

logger.LogInformation("Published MyOtherEvent");
}
catch (OperationCanceledException)
{
// graceful shutdown
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"AzureServiceBusConnectionString": "<PLACEHOLDER>"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NServiceBus;

namespace Shared
{
public class MyEvent : IEvent
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using NServiceBus;

namespace Shared;

public class MyOtherEvent : IEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>13.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" Version="9.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NServiceBus;
using Shared;

namespace SubscriberV4
{
public class MyOtherEventHandler : IHandleMessages<MyOtherEvent>
{
private readonly ILogger<MyOtherEventHandler> logger;

public MyOtherEventHandler(ILogger<MyOtherEventHandler> logger)
{
this.logger = logger;
}

public Task Handle(MyOtherEvent message, IMessageHandlerContext context)
{
logger.LogInformation("Received MyOtherEvent");
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;
using Shared;

namespace SubscriberV4
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Subscriber 2 running Azure Service Bus transport V4");
var builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Logging.AddConsole();

var endpointConfiguration = new EndpointConfiguration("Subscriber2");

var connectionString = builder.Configuration.GetConnectionString("AzureServiceBusConnectionString");
var routing = endpointConfiguration.UseTransport(new AzureServiceBusTransport(connectionString));
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

// Operational scripting: https://docs.particular.net/transports/azure-service-bus/operational-scripting
endpointConfiguration.EnableInstallers();

builder.UseNServiceBus(endpointConfiguration);

var host = builder.Build();
await host.RunAsync();
}
}
}
Loading

0 comments on commit 5563572

Please sign in to comment.