AugusteVN.Azure.ServiceBus 1.0.3

dotnet add package AugusteVN.Azure.ServiceBus --version 1.0.3                
NuGet\Install-Package AugusteVN.Azure.ServiceBus -Version 1.0.3                
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="AugusteVN.Azure.ServiceBus" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AugusteVN.Azure.ServiceBus --version 1.0.3                
#r "nuget: AugusteVN.Azure.ServiceBus, 1.0.3"                
#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 AugusteVN.Azure.ServiceBus as a Cake Addin
#addin nuget:?package=AugusteVN.Azure.ServiceBus&version=1.0.3

// Install AugusteVN.Azure.ServiceBus as a Cake Tool
#tool nuget:?package=AugusteVN.Azure.ServiceBus&version=1.0.3                

Azure Service Bus

Publishing messages to- & receiving messages from the Azure Service Bus.

This package contains wrapper- & helper functions for the official Azure.Messaging.ServiceBus NuGet package.

Contains:

  • SenderClient to send a message to a topic.
  • ReceiverClient to receive messages from a topic subscription.

Configuration

appsettings.json

{
  "ServiceBusConfig": {
    "ConnectionString": "<to-fill>",
    "TopicName": "<to-fill>",
    "SubscriptionName": "<optional-only-needed-for-receiving>"
  }
}

Program.cs

builder.Services
    .AddOptions<ServiceBusConfig>()
    .BindConfiguration(nameof(ServiceBusConfig));

services.AddSingleton<ISenderClient, SenderClient>();
services.AddSingleton<IReceiverClient, ReceiverClient>();

// Or initialize an instance of either.

var sender = new SenderClient(
    new ServiceBusConfig {
        ConnectionString = "<to-fill>",
        TopicName = "<to-fill>"
    });

var receiver = new ReceiverClient(
    new ServiceBusConfig {
        ConnectionString = "<to-fill>",
        TopicName = "<to-fill>",
        SubscriptionName = "<to-fill>"
    });

Send Message

Program.cs

app.MapPost("/", async (ISenderClient sender) => {
    
    var data = new { Foo = "bar" };
    var customProperties = new Dictionary<string, object> {{ "foo", "bar" }};
    
    await sender.SendAsync(data, customProperties);
    
    // Or use the overload and construct your own 'ServiceBusMessage'.
    await sender.SendAsync(new ServiceBusMessage(body: data) {
        CorrelationId = "",
        ApplicationProperties = customProperties,
        ...
    });
    
    return Results.NoContent();
});

Receive Message

Program.cs

app.MapPost("/", async (IReceiverClient receiver) => {
    
    var message = await receiver.ReceiveMessageAsync(
        maxWaitTime: TimeSpan.FromMinutes(5));
    
    if (message != null) {
        await receiver.CompleteMessageAsync(message);
    }
    
    return Results.NoContent();
});

Receive Multiple Messages

Program.cs

app.MapPost("/", async (IReceiverClient receiver) => {
    
    var messages = await receiver.ReceiveMessagesAsync(
        maxMessages: 300,
        maxWaitTime: TimeSpan.FromMinutes(5));
    
    foreach(var message in messages) {
        var isWantedMessage = message.ApplicationProperties["foo"]?.ToString() == "bar";
        if (!isWantedMessage) continue;

        var messageBody = receiver.ParseMessageBody<object>(message);
        
        ...
        
        var isSuccess = await DoStuff(messageBody);
        if (!isSuccess) continue;
            
        await receiver.CompleteMessageAsync(message);    
    }
    
    return Results.NoContent();
});

To see it in action, watch: Azure Service Bus Messaging in C# .NET

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.3 131 4/27/2024
1.0.2 306 12/26/2023
1.0.1 245 11/18/2023
1.0.0 120 10/27/2023

Add config validation.