Skip to content

Commit

Permalink
Update existing samples to ASB v5 (#6990)
Browse files Browse the repository at this point in the history
* Update native integration sample

* Update send-reply sample

* Update native integration pubsub sample

* Update send and receive with nservicebus sample

* Mark as prerelease

* Add topology migration sample to index

---------

Co-authored-by: Daniel Marbach <[email protected]>
  • Loading branch information
danielmarbach and danielmarbach authored Feb 14, 2025
1 parent da1c4f0 commit aa3e362
Show file tree
Hide file tree
Showing 66 changed files with 1,212 additions and 236 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 15.0.26730.12
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{0D0EF536-3C89-4191-A0EF-C0742236FC46}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeSubscriberB", "NativeSubscriberB\NativeSubscriberB.csproj", "{D32FE422-7DA6-484C-91C2-2E48AE014040}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeSubscriberA", "NativeSubscriberA\NativeSubscriberA.csproj", "{9B7F1E99-B489-4432-923F-2C803480FC37}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeShared", "NativeShared\NativeShared.csproj", "{EF53E157-CE4B-497C-B53E-0102D4D4D7FF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Publisher", "Publisher\Publisher.csproj", "{96A0B2AD-FEAC-4F43-9811-D99420D1CCC3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D0EF536-3C89-4191-A0EF-C0742236FC46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D0EF536-3C89-4191-A0EF-C0742236FC46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D0EF536-3C89-4191-A0EF-C0742236FC46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D0EF536-3C89-4191-A0EF-C0742236FC46}.Release|Any CPU.Build.0 = Release|Any CPU
{D32FE422-7DA6-484C-91C2-2E48AE014040}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D32FE422-7DA6-484C-91C2-2E48AE014040}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D32FE422-7DA6-484C-91C2-2E48AE014040}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D32FE422-7DA6-484C-91C2-2E48AE014040}.Release|Any CPU.Build.0 = Release|Any CPU
{9B7F1E99-B489-4432-923F-2C803480FC37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9B7F1E99-B489-4432-923F-2C803480FC37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9B7F1E99-B489-4432-923F-2C803480FC37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9B7F1E99-B489-4432-923F-2C803480FC37}.Release|Any CPU.Build.0 = Release|Any CPU
{EF53E157-CE4B-497C-B53E-0102D4D4D7FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF53E157-CE4B-497C-B53E-0102D4D4D7FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF53E157-CE4B-497C-B53E-0102D4D4D7FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF53E157-CE4B-497C-B53E-0102D4D4D7FF}.Release|Any CPU.Build.0 = Release|Any CPU
{96A0B2AD-FEAC-4F43-9811-D99420D1CCC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96A0B2AD-FEAC-4F43-9811-D99420D1CCC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96A0B2AD-FEAC-4F43-9811-D99420D1CCC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96A0B2AD-FEAC-4F43-9811-D99420D1CCC3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0348CF9-2853-4649-B5A2-A29A74386A2A}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.*" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;

namespace NativeSender
{
using System;
using System.Threading.Tasks;

public static class TopologyManager
{
public static async Task CreateSubscription(string connectionString, string subscriptionName, string ruleName, SqlRuleFilter sqlFilter, string topicPath, string forwardTo)
{
var client = new ServiceBusAdministrationClient(connectionString);

try
{
await client.CreateQueueAsync(forwardTo);
}
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
{
}

try
{
await client.CreateTopicAsync(topicPath);
}
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
{
}

try
{
#region SubscriptionCreation

await client.CreateSubscriptionAsync(new CreateSubscriptionOptions(topicPath, subscriptionName)
{
LockDuration = TimeSpan.FromMinutes(5),
EnableDeadLetteringOnFilterEvaluationExceptions = false,
MaxDeliveryCount = int.MaxValue,
EnableBatchedOperations = true,
ForwardTo = forwardTo,
}, new CreateRuleOptions(ruleName, sqlFilter));

#endregion
}
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
{
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NativeShared\NativeShared.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
using NativeSender;

static class Program
{
const string EnclosedMessageTypesHeader = "NServiceBus.EnclosedMessageTypes";

static string ConnectionString
{
get
{
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.");
}

return connectionString;
}
}

static async Task Main()
{
var queueName = "Samples.ASB.NativeIntegration.NativeSubscriberA";

Console.Title = queueName;

#region EventOneSubscription
await TopologyManager.CreateSubscription(
ConnectionString,
queueName,
ruleName:"$default",
sqlFilter: new TrueRuleFilter(),
topicPath: "EventOne",
forwardTo: queueName
);
#endregion

var serviceBusClient = new ServiceBusClient(ConnectionString);
var serviceBusProcessor = serviceBusClient.CreateProcessor("Samples.ASB.NativeIntegration.NativeSubscriberA");

serviceBusProcessor.ProcessMessageAsync += MessageHandler;
serviceBusProcessor.ProcessErrorAsync += ErrorHandler;

await serviceBusProcessor.StartProcessingAsync();

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

static async Task MessageHandler(ProcessMessageEventArgs args)
{
var messageType = (string) args.Message.ApplicationProperties[EnclosedMessageTypesHeader];
var bodyJson = Encoding.UTF8.GetString(args.Message.Body.ToArray());

Console.WriteLine($"Received: {messageType}");
Console.WriteLine(bodyJson);

// complete the message. messages is deleted from the subscription.
await args.CompleteMessageAsync(args.Message);
}

static Task ErrorHandler(ProcessErrorEventArgs args)
{
Console.WriteLine(args.Exception.ToString());
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>13.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NativeShared\NativeShared.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
using NativeSender;

static class Program
{
const string EnclosedMessageTypesHeader = "NServiceBus.EnclosedMessageTypes";

static string ConnectionString
{
get
{
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.");
}

return connectionString;
}
}

static async Task Main()
{
var queueName = "Samples.ASB.NativeIntegration.NativeSubscriberB";

Console.Title = queueName;

await TopologyManager.CreateSubscription(
ConnectionString,
queueName,
ruleName:"$default",
sqlFilter: new TrueRuleFilter(),
topicPath: "EventOne",
forwardTo: queueName
);
await TopologyManager.CreateSubscription(
ConnectionString,
queueName,
ruleName:"$default",
sqlFilter: new TrueRuleFilter(),
topicPath: "EventTwo",
forwardTo: queueName
);

var serviceBusClient = new ServiceBusClient(ConnectionString);
var serviceBusProcessor = serviceBusClient.CreateProcessor(queueName);

serviceBusProcessor.ProcessMessageAsync += MessageHandler;
serviceBusProcessor.ProcessErrorAsync += ErrorHandler;

await serviceBusProcessor.StartProcessingAsync();

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

static async Task MessageHandler(ProcessMessageEventArgs args)
{
var messageType = (string) args.Message.ApplicationProperties[EnclosedMessageTypesHeader];
var bodyJson = Encoding.UTF8.GetString(args.Message.Body.ToArray());

Console.WriteLine($"Received: {messageType}");
Console.WriteLine(bodyJson);

// complete the message. messages is deleted from the subscription.
await args.CompleteMessageAsync(args.Message);
}

static Task ErrorHandler(ProcessErrorEventArgs args)
{
Console.WriteLine(args.Exception.ToString());
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Threading.Tasks;
using NServiceBus;

Console.Title = "Publisher";

var endpointConfiguration = new EndpointConfiguration("Samples.ASB.Publisher");

endpointConfiguration.EnableInstallers();
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.Conventions().DefiningEventsAs(type => type.Name == nameof(EventTwo) || type.Name == nameof(EventOne));


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

var endpointInstance = await Endpoint.Start(endpointConfiguration);
Console.WriteLine("Press any key to publish events");
Console.ReadKey();
Console.WriteLine();

await endpointInstance.Publish(new EventOne
{
Content = $"{nameof(EventOne)} sample content",
PublishedOnUtc = DateTime.UtcNow
});

await endpointInstance.Publish(new EventTwo
{
Content = $"{nameof(EventTwo)} sample content",
PublishedOnUtc = DateTime.UtcNow
});

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,16 @@
<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>
<ItemGroup Label="Transitive dependencies">
<PackageReference Include="NServiceBus" Version="9.*" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

public class EventOne
{
public string Content { get; set; }
public DateTime PublishedOnUtc { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

public class EventTwo
{
public string Content { get; set; }
public DateTime PublishedOnUtc { get; set; }
}
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="Azure.Messaging.ServiceBus" Version="7.*" />
</ItemGroup>
</Project>
Empty file.
Loading

0 comments on commit aa3e362

Please sign in to comment.