Atc.Azure.Messaging 1.0.43

There is a newer version of this package available.
See the version list below for details.
dotnet add package Atc.Azure.Messaging --version 1.0.43                
NuGet\Install-Package Atc.Azure.Messaging -Version 1.0.43                
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="Atc.Azure.Messaging" Version="1.0.43" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Atc.Azure.Messaging --version 1.0.43                
#r "nuget: Atc.Azure.Messaging, 1.0.43"                
#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 Atc.Azure.Messaging as a Cake Addin
#addin nuget:?package=Atc.Azure.Messaging&version=1.0.43

// Install Atc.Azure.Messaging as a Cake Tool
#tool nuget:?package=Atc.Azure.Messaging&version=1.0.43                

NuGet Version

ATC Azure Messaging

A .NET component library for publishing messages on Azure Event Hubs and Service Bus. This targets .NET Standard 2.1 and depends on ATC Azure Options.

Getting Started

The first thing you need to do to make sure that your configuration is setup correctly. For the sake of simplicity, let's use the appsettings.json file created with every .NET project

The following is an example of how your would configure your EventHub or ServiceBus for ATC Azure Options

{  
  "EventHubOptions": {
    "ConnectionString": "Endpoint=sb://[your eventhub namespace].servicebus.windows.net/;SharedAccessKeyName=[eventhub name];SharedAccessKey=[sas key]"    
  },
  "ServiceBusOptions": {
    "ConnectionString": "Endpoint=sb://[your servicebus namespace].servicebus.windows.net/;SharedAccessKeyName=[topic|queue name];SharedAccessKey=[sas key]"
  }
}

When binding to KeyVault this should of course be EventHubOptions--ConnectionString for EventHub or ServiceBusOptions--ConnectionString for ServiceBus

The next step is to register the dependencies required to use this component library. For this you can use the IServiceCollection extension method ConfigureMessagingServices(IConfiguration). This piggy backs over the Microsoft.Extensions.DependencyInjection namespace so that you don't need to include anything extra using statements

Here's an example of how to do this using a Minimal API setup

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Register Atc.Azure.Messaging dependencies
builder.Services.ConfigureMessagingServices(builder.Configuration);

Publishing to EventHub

To publish events to an EventHub you need an instance of IEventHubPublisher, this can be constructed via the IEventHubPublisherFactory which exposes the Create(string eventHubName) method

internal class FooPublisher 
{
    private readonly IEventHubPublisher publisher;

    public FooPublisher(IEventHubPublisherFactory factory)
    {
        publisher = factory.Create([existing eventhub name]);
    }

    public Task Publish(object message)
        => publisher.PublishAsync(message);
}

Publishing to ServiceBus

To publish events to a ServiceBus topic or queue you need an instance of IServiceBusPublisher

internal class BarPublisher 
{
    private readonly IServiceBusPublisher publisher;

    public BarPublisher(IServiceBusPublisher publisher)
    {
        this.publisher = publisher;
    }

    public Task Publish(object message)
        => publisher.PublishAsync("[existing servicebus topic]", message);
}

Here's a full example of how to use the publishers above using a Minimal API setup (SwaggerUI enabled) with a single endpoint called POST /data that accepts a simple request body { "a": "string", "b": "string", "c": "string" } which publishes the request to an EventHub and a ServiceBus topic

using Atc.Azure.Messaging.EventHub;
using Atc.Azure.Messaging.ServiceBus;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<SendDataHandler>();

// Register Atc.Azure.Messaging dependencies
builder.Services.ConfigureMessagingServices(builder.Configuration);

var app = builder.Build();
app.UseHttpsRedirection();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapPost(
    "/data",
    (Request request, SendDataHandler handler) => handler.Post(request));

app.Run();

#pragma warning disable MA0048 // File name must match type name
#pragma warning disable SA1649 // File name should match first type name
#pragma warning disable MA0047 // Declare types in namespaces
#pragma warning disable S3903 // Types should be defined in named namespaces

internal class SendDataHandler
{
    private readonly IEventHubPublisher eventHubPublisher;
    private readonly IServiceBusPublisher serviceBusPublisher;

    public SendDataHandler(
        IEventHubPublisherFactory eventHubFactory,
        IServiceBusPublisher serviceBusPublisher)
    {
        eventHubPublisher = eventHubFactory.Create("[existing eventhub");
        this.serviceBusPublisher = serviceBusPublisher;
    }

    public async Task<Response> Post(Request request)
    {
        await eventHubPublisher.PublishAsync(request);

        await serviceBusPublisher.PublishAsync("existing topic|queue", request);

        return new Response(
            Guid.NewGuid().ToString("N"),
            request.A,
            request.B,
            request.C);
    }
}

internal record Request(string A, string B, string C);

internal record Response(string Id, string A, string B, string C);

The workflow setup for this repository

Read more on Git-Flow

How to contribute

Contribution Guidelines

Coding Guidelines

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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.58 1,523 6/13/2023
1.0.52 525 1/24/2023
1.0.43 423 6/17/2022
1.0.16 436 6/15/2022