Ex.Microsoft.Azure.ServiceBus.Hosting implements the ability to host an application process messages from an Azure Service Bus Queue or Subscription.
Released under the MIT License. See the LICENSE File for further details.
Use the IMessageHandler
and IExceptionHandler
interfaces in order to implement functionality for processing. The IMessageHandler
interface is used for business logic that would normally get processed through the RegisterMessageHandler
method. Likewise the IExceptionHandler
interface is used for handling any uncaught exceptions.
This sample shows a simple implementation of what IMessageHandler
requires.
using Microsoft.Azure.ServiceBus.Hosting;
using System.Threading;
using System.Threading.Tasks;
public class MyMessageHandler : IMessageHandler {
public Task HandleAsync(Message message, CancellationToken cancellationToken) =>
throw new NotImplementedException();
}
This sample shows a simple implementation of what IExceptionHandler
requires.
using Microsoft.Azure.ServiceBus.Hosting;
using System.Threading;
using System.Threading.Tasks;
public class MyExceptionHandler : IExceptionHandler {
public Task HandleExceptionAsync(ExceptionReceivedEventArgs eventArgs) =>
throw new NotImplementedException();
}
Use the ConfigureServiceBusQueue
extension method to connect to a Service Bus Queue.
using Microsoft.Azure.ServiceBus.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
public static class Program {
static string GetConnectionString(IConfiguration configuration) =>
configuration.GetSection("ConnectionStrings")["ServiceBusConnection"];
public static Task Main(string[] args) =>
new HostBuilder().ConfigureServiceBusQueue(
(hostBuilder, options) =>
options.ConnectionString = GetConnectionString(hostBuilder.Configuration),
context => context.ExceptionHandler<MyExceptionHandler>()
.StaticMessageHandler()
.Scoped<MyMessageHandler>()
)
.RunConsoleAsync();
}
Use the ConfigureServiceBusSubscription
extension method to connect to a Service Bus Subscription.
using Microsoft.Azure.ServiceBus.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
public static class Program {
static string GetConnectionString(IConfiguration configuration) =>
configuration.GetSection("ConnectionStrings")["ServiceBusConnection"];
public static Task Main(string[] args) =>
new HostBuilder().ConfigureServiceBusSubscription(
(hostBuilder, options) =>
{
options.ConnectionString = GetConnectionString(hostBuilder.Configuration);
options.SubscriptionName = hostBuilder.Configuration["SubscriptionName"];
},
context => context.ExceptionHandler<MyExceptionHandler>()
.StaticMessageHandler()
.Scoped<MyMessageHandler>()
)
.RunConsoleAsync();
}
You can also handle messages using IMessageHandler<T>
, which creates a contract with a generic type.
public class MyInt32Handler : IMessageHandler<int> {
public Task HandleAsync(int message, CancellationToken cancellationToken) =>
throw new NotImplementedException();
}
public class MyStringHandler : IMessageHandler<string> {
public Task HandleAsync(string message, CancellationToken cancellationToken) =>
throw new NotImplementedException();
}
public static class Program {
static string GetConnectionString(IConfiguration configuration) =>
configuration.GetSection("ConnectionStrings")["ServiceBusConnection"];
public static Task Main(string[] args) =>
new HostBuilder().ConfigureServiceBusQueue(
(hostBuilder, options) =>
options.ConnectionString = GetConnectionString(hostBuilder.Configuration),
context => context.ExceptionHandler<MyExceptionHandler>()
.GenericMessageHandler(msg =>
{
switch (msg.ContentType)
{
case "int":
return BitConverter.ToInt32(msg.Body, 0);
case "string":
return Encoding.UTF8.GetString(msg.Body);
default:
throw new Exception("Invalid message content type");
}
})
.Scoped<int, MyInt32Handler>()
.Scoped<string, MyStringHandler>()
)
.RunConsoleAsync();
}