Cabazure.Messaging.ServiceBus 1.0.0

dotnet add package Cabazure.Messaging.ServiceBus --version 1.0.0                
NuGet\Install-Package Cabazure.Messaging.ServiceBus -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cabazure.Messaging.ServiceBus" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cabazure.Messaging.ServiceBus --version 1.0.0                
#r "nuget: Cabazure.Messaging.ServiceBus, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cabazure.Messaging.ServiceBus as a Cake Addin
#addin nuget:?package=Cabazure.Messaging.ServiceBus&version=1.0.0

// Install Cabazure.Messaging.ServiceBus as a Cake Tool
#tool nuget:?package=Cabazure.Messaging.ServiceBus&version=1.0.0                

GitHub Actions Workflow Status GitHub Release Date NuGet Version NuGet Downloads

Branch Coverage Line Coverage Method Coverage

Cabazure.Messaging

Cabazure.Messaging is a set of libraries for handling message publishing and processing on Azure EventHub and ServiceBus.

The following packages are produced from this repository:

  • Cabazure.Messaging.Abstrations - Publisher and processor abstractions
  • Cabazure.Messaging.EventHub - Package for targeting Event Hub
  • Cabazure.Messaging.ServiceBus - Package for targeting Service Bus

Getting started

1. Add package reference

For targeting Azure Event Hub, add a reference to the Cabazure.Messaging.EventHub package. Please see the Configure EventHub Connection section for how to setup the Event Hub package.

For targeting Azure Service Bus, add a reference to the Cabazure.Messaging.ServiceBus package. Please see the Configure ServiceBus Connection section for how to setup the Service Bus package.

2a. Configure EventHub connection

Cabazure.Messaging.EventHub is configured by calling the AddCabazureEventHub() on the IServiceCollection during startup of your application, like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCabazureEventHub(b => b
    .Configure(o => o
        .WithSerializerOptions(JsonSerializerOptions.Web)
        .WithConnection(
            "eventhub1.servicebus.windows.net",
            new DefaultAzureCredential())
        .WithBlobStorage(
            new Uri("https://account1.blob.core.windows.net/container1"),
            new DefaultAzureCredential(),
            createIfNotExist: true)));

The CabazureEventHubOptions can also be configured using the Microsoft.Extensions.Options framework, by implementing IConfigureOptions<CabazureEventHubOptions>, and pass it to the Configure<T>() overload.

Multiple Event Hub connections are supported by passing a connectionName to the AddCabazureEventHub() method.

The Blob Storage configuration is only required if you need to run a message processor, as the storage is used for processor state.

2b. Configure ServiceBus connection

Cabazure.Messaging.ServiceBus is configured by calling the AddCabazureServiceBus() on the IServiceCollection during startup of your application, like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCabazureServiceBus(b => b
    .Configure(o => o
        .WithSerializerOptions(JsonSerializerOptions.Web)
        .WithConnection(
            "servicebus1.servicebus.windows.net",
            new DefaultAzureCredential()));

The CabazureServiceBusOptions can also be configured using the Microsoft.Extensions.Options framework, by implementing IConfigureOptions<CabazureServiceBusOptions>, and pass it to the Configure<T>() overload.

Multiple Service Bus connections are supported by passing a connectionName to the AddCabazureServiceBus() method.

3. Add a Publisher

To add a message publisher, the AddPublisher<TMessage>() method is called on the EventHubBuilder or ServiceBusBuilder, like this:

// Event Hub publisher
builder.Services.AddCabazureEventHub(b => b
    .AddPublisher<MyEvent>("eventHub1");

// Service Bus publisher
builder.Services.AddCabazureServiceBus(b => b
    .AddPublisher<MyEvent>("topic1");

This will register an IMessagePublisher<MyEvent> service in the dependency injection container, allowing you to publish MyEvent messages using the PublishAsync() methods.

An optional builder can be passed to the AddPublisher<TMessage>() method. This can be used to setup default properties and partition key used when publishing, like this:

// Event Hub publisher
builder.Services.AddCabazureEventHub(b => b
    .AddPublisher<MyEvent>("eventHub1",
    b => b
      .WithPartitionKey(m => m.PartitionKey)
      .WithProperty("Property1", m => m.Property1));

// Service Bus publisher
builder.Services.AddCabazureServiceBus(b => b
    .AddPublisher<MyEvent>("topic1",
    b => b
      .WithPartitionKey(m => m.PartitionKey)
      .WithProperty("Property1", m => m.Property1));

Properties and partition key can also be specified using the PublishingOptions that can be passed to the IMessagePublisher<TMessage>.PublishAsync() method when publishing messages.

4. Add a Processor

To add a processor, the IMessageProcessor<TMessage> should be implemented and registered by calling the AddProcessor<TMessage, TProcessor>() method on the EventHubBuilder or ServiceBusBuilder, like this:

// Event Hub publisher
builder.Services.AddCabazureEventHub(b => b
    .AddProcessor<MyEvent, MyEventProcessor>(
        "eventhub1"
        "consumerGroup1");

// Service Bus publisher
builder.Services.AddCabazureServiceBus(b => b
    .AddProcessor<MyEvent, MyEventProcessor>(
        "topic1",
        "subscription1");

This will register an IHostedService that will start up with the application, and pass deserialized MyEvent messages to the MyEventProcessor.

If needed, the processor can be started and stopped by using the registered IMessageProcessorService<MyEventProcessor> service.

An optional builder can be passed to the AddProcessor<TMessage, TProcessor>() method. This can be used to configure processor options and configure property-based filters, like this:

// Event Hub publisher
builder.Services.AddCabazureEventHub(b => b
    .AddProcessor<MyEvent, MyEventProcessor>(
        "eventhub1"
        "consumerGroup1",
        b => b
          .WithProcessorOptions(new EventProcessorClientOptions
          {
              PrefetchCount = 300,
          })
          .WithFilter(p => p.TryGetValue("Property1", out var v) && v is "Value1")));

// Service Bus publisher
builder.Services.AddCabazureServiceBus(b => b
    .AddProcessor<MyEvent, MyEventProcessor>(
        "topic1",
        "subscription1",
        b => b
          .WithProcessorOptions(new ServiceBusProcessorOptions
          {
              MaxConcurrentCalls = 16,
              PrefetchCount = 64,
          })
          .WithFilter(p => p.TryGetValue("Property1", out var v) && v is "Value1")));

Samples

Please see the samples folder for sample implementation of publishers and processors targeting both EventHub and ServiceBus.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 59 11/18/2024
0.1.0 59 11/16/2024