|
3 | 3 |
|
4 | 4 | # ATC Azure Messaging |
5 | 5 |
|
6 | | -A component library for publishing messages on Azure Event Hubs and Service Bus. |
| 6 | +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](https://github.com/atc-net/atc-azure-options). |
| 7 | + |
| 8 | +# Getting Started |
| 9 | + |
| 10 | +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 |
| 11 | + |
| 12 | +The following is an example of how your would configure your EventHub or ServiceBus for [ATC Azure Options](https://github.com/atc-net/atc-azure-options) |
| 13 | + |
| 14 | +```json |
| 15 | +{ |
| 16 | + "EventHubOptions": { |
| 17 | + "ConnectionString": "Endpoint=sb://[your eventhub namespace].servicebus.windows.net/;SharedAccessKeyName=[eventhub name];SharedAccessKey=[sas key]" |
| 18 | + }, |
| 19 | + "ServiceBusOptions": { |
| 20 | + "ConnectionString": "Endpoint=sb://[your servicebus namespace].servicebus.windows.net/;SharedAccessKeyName=[topic|queue name];SharedAccessKey=[sas key]" |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +When binding to KeyVault this should of course be `EventHubOptions--ConnectionString` for EventHub or `ServiceBusOptions--ConnectionString` for ServiceBus |
| 26 | + |
| 27 | +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 |
| 28 | + |
| 29 | +Here's an example of how to do this using a Minimal API setup |
| 30 | + |
| 31 | +```csharp |
| 32 | +var builder = WebApplication.CreateBuilder(args); |
| 33 | +builder.Services.AddEndpointsApiExplorer(); |
| 34 | +builder.Services.AddSwaggerGen(); |
| 35 | + |
| 36 | +// Register Atc.Azure.Messaging dependencies |
| 37 | +builder.Services.ConfigureMessagingServices(builder.Configuration); |
| 38 | +``` |
| 39 | + |
| 40 | +## Publishing to EventHub |
| 41 | + |
| 42 | +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 |
| 43 | + |
| 44 | +```csharp |
| 45 | +internal class FooPublisher |
| 46 | +{ |
| 47 | + private readonly IEventHubPublisher publisher; |
| 48 | + |
| 49 | + public FooPublisher(IEventHubPublisherFactory factory) |
| 50 | + { |
| 51 | + publisher = factory.Create([existing eventhub name]); |
| 52 | + } |
| 53 | + |
| 54 | + public Task Publish(object message) |
| 55 | + => publisher |
| 56 | + .PublishAsync( |
| 57 | + bar, |
| 58 | + new Dictionary<string, string> |
| 59 | + { |
| 60 | + {"messageType", nameof(message)} |
| 61 | + } |
| 62 | + ) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Publishing to ServiceBus |
| 67 | + |
| 68 | +To publish events to a ServiceBus topic or queue you need an instance of `IServiceBusPublisher` |
| 69 | + |
| 70 | +```csharp |
| 71 | +internal class BarPublisher |
| 72 | +{ |
| 73 | + private readonly IServiceBusPublisher publisher; |
| 74 | + |
| 75 | + public BarPublisher(IServiceBusPublisher publisher) |
| 76 | + { |
| 77 | + this.publisher = publisher; |
| 78 | + } |
| 79 | + |
| 80 | + public Task Publish(object message) |
| 81 | + => publisher |
| 82 | + .PublishAsync( |
| 83 | + "[existing servicebus topic]", |
| 84 | + "[session id or nothing]", |
| 85 | + JsonSerializer.Serialize(message)); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +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 |
| 90 | + |
| 91 | +```csharp |
| 92 | +var builder = WebApplication.CreateBuilder(args); |
| 93 | +builder.Services.AddEndpointsApiExplorer(); |
| 94 | +builder.Services.AddSwaggerGen(); |
| 95 | +builder.Services.AddSingleton<SendDataHandler>(); |
| 96 | + |
| 97 | +// Register Atc.Azure.Messaging dependencies |
| 98 | +builder.Services.ConfigureMessagingServices(builder.Configuration); |
| 99 | + |
| 100 | +var app = builder.Build(); |
| 101 | +app.UseHttpsRedirection(); |
| 102 | +if (app.Environment.IsDevelopment()) |
| 103 | +{ |
| 104 | + app.UseSwagger(); |
| 105 | + app.UseSwaggerUI(); |
| 106 | +} |
| 107 | + |
| 108 | +app.MapPost( |
| 109 | + "/data", |
| 110 | + (Request request, SendDataHandler handler) => handler.Post(request)); |
| 111 | + |
| 112 | +app.Run(); |
| 113 | + |
| 114 | +internal class SendDataHandler |
| 115 | +{ |
| 116 | + private readonly IEventHubPublisher eventHubPublisher; |
| 117 | + private readonly IServiceBusPublisher serviceBusPublisher; |
| 118 | + |
| 119 | + public SendDataHandler( |
| 120 | + IEventHubPublisherFactory eventHubFactory, |
| 121 | + IServiceBusPublisher serviceBusPublisher) |
| 122 | + { |
| 123 | + eventHubPublisher = eventHubFactory.Create("[existing eventhub]"); |
| 124 | + this.serviceBusPublisher = serviceBusPublisher; |
| 125 | + } |
| 126 | + |
| 127 | + public async Task<Response> Post(Request request) |
| 128 | + { |
| 129 | + await eventHubPublisher |
| 130 | + .PublishAsync( |
| 131 | + request, |
| 132 | + new Dictionary<string, string>(StringComparer.Ordinal) |
| 133 | + { |
| 134 | + { "MessageType", "example" }, |
| 135 | + }); |
| 136 | + |
| 137 | + await serviceBusPublisher |
| 138 | + .PublishAsync( |
| 139 | + "[existing topic|queue", |
| 140 | + "[session id or nothing]", |
| 141 | + System.Text.Json.JsonSerializer.Serialize(request)); |
| 142 | + |
| 143 | + return new Response( |
| 144 | + Guid.NewGuid().ToString("N"), |
| 145 | + request.A, |
| 146 | + request.B, |
| 147 | + request.C); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +internal record Request(string A, string B, string C); |
| 152 | + |
| 153 | +internal record Response(string Id, string A, string B, string C); |
| 154 | +``` |
7 | 155 |
|
8 | 156 |
|
9 | 157 | # The workflow setup for this repository |
|
0 commit comments