MVFC.Messaging.Azure 3.0.2

dotnet add package MVFC.Messaging.Azure --version 3.0.2
                    
NuGet\Install-Package MVFC.Messaging.Azure -Version 3.0.2
                    
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="MVFC.Messaging.Azure" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MVFC.Messaging.Azure" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="MVFC.Messaging.Azure" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MVFC.Messaging.Azure --version 3.0.2
                    
#r "nuget: MVFC.Messaging.Azure, 3.0.2"
                    
#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.
#:package MVFC.Messaging.Azure@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MVFC.Messaging.Azure&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=MVFC.Messaging.Azure&version=3.0.2
                    
Install as a Cake Tool

MVFC.Messaging.Azure

🇧🇷 Leia em Português

CI codecov License Platform NuGet Version NuGet Downloads

A .NET messaging provider for Azure Service Bus, built on top of MVFC.Messaging.Core. Provides ServiceBusPublisher<T> and ServiceBusConsumer<T> for publishing and consuming JSON-serialized messages in queues and topics with AMQP/TCP transport.

Package

Package Downloads
MVFC.Messaging.Azure Downloads

Installation

dotnet add package MVFC.Messaging.Azure

This package depends on MVFC.Messaging.Core (installed automatically) and Azure.Messaging.ServiceBus.

Configuration

Connection String

Both ServiceBusPublisher<T> and ServiceBusConsumer<T> accept a connection string and a queue or topic name in their constructor. You can find the connection string in the Azure Portal under your Service Bus namespace → Shared access policies.

Endpoint=sb://my-namespace.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your-key>

Transport

The provider is configured to use AMQP over TCP (ServiceBusTransportType.AmqpTcp) for optimal performance. This is a direct TCP connection — no WebSocket overhead.

appsettings.json Example

{
  "Azure": {
    "ServiceBus": {
      "ConnectionString": "Endpoint=sb://my-namespace.servicebus.windows.net/;SharedAccessKeyName=...",
      "QueueName": "orders"
    }
  }
}
var connectionString = builder.Configuration["Azure:ServiceBus:ConnectionString"]!;
var queueName = builder.Configuration["Azure:ServiceBus:QueueName"]!;

Usage

Publishing a Single Message

using MVFC.Messaging.Azure.ServiceBus;

var connectionString = "Endpoint=sb://my-namespace.servicebus.windows.net/;...";
var queueName = "orders";

await using var publisher = new ServiceBusPublisher<OrderCreated>(connectionString, queueName);

var order = new OrderCreated(1, "Keyboard", 149.90m);
await publisher.PublishAsync(order);

The message is serialized to JSON and wrapped in a ServiceBusMessage.

Publishing a Batch

The publisher uses the native ServiceBusMessageBatch API for batch operations. If a message cannot fit in the batch (size limit reached), an InvalidOperationException is thrown:

var orders = new[]
{
    new OrderCreated(1, "Keyboard", 149.90m),
    new OrderCreated(2, "Mouse", 59.90m),
    new OrderCreated(3, "Monitor", 899.00m)
};

await publisher.PublishBatchAsync(orders);

Consuming Messages

The consumer uses Azure Service Bus's built-in ServiceBusProcessor, which manages message polling, concurrency, and error handling internally. Messages are not auto-completed — they are explicitly completed after successful handler execution:

using MVFC.Messaging.Azure.ServiceBus;

var connectionString = "Endpoint=sb://my-namespace.servicebus.windows.net/;...";
var queueName = "orders";

await using var consumer = new ServiceBusConsumer<OrderCreated>(connectionString, queueName);

await consumer.StartAsync(async (message, ct) =>
{
    Console.WriteLine($"Processing order #{message.OrderId}: {message.Product}");
    // Your business logic here
}, cancellationToken);

// ... later, when shutting down:
await consumer.StopAsync();

Consumer behavior:

  • Uses the ServiceBusProcessor event-driven model — no manual polling needed.
  • AutoCompleteMessages is set to false; messages are completed explicitly after handler success.
  • If the handler throws an exception, the message is not completed and will be retried based on the queue's max delivery count.
  • Error events are logged to Console.WriteLine (you can customize this by extending the class).
  • StartAsync starts the processor; StopAsync stops it gracefully.

Complete Publish + Consume Example

using MVFC.Messaging.Azure.ServiceBus;

var connectionString = "Endpoint=sb://my-namespace.servicebus.windows.net/;...";
var queueName = "orders";

await using var publisher = new ServiceBusPublisher<OrderCreated>(connectionString, queueName);
await using var consumer = new ServiceBusConsumer<OrderCreated>(connectionString, queueName);

// Start consuming
var received = new TaskCompletionSource<OrderCreated>();
await consumer.StartAsync(async (msg, ct) =>
{
    Console.WriteLine($"Received: Order #{msg.OrderId} — {msg.Product}");
    received.SetResult(msg);
}, CancellationToken.None);

// Publish
await publisher.PublishAsync(new OrderCreated(42, "Keyboard", 149.90m));

// Wait for the message to be consumed
var result = await received.Task.WaitAsync(TimeSpan.FromSeconds(30));

// Cleanup
await consumer.StopAsync();

API Reference

ServiceBusPublisher<T>

Constructor Parameters
ServiceBusPublisher<T>(string connectionString, string queueOrTopicName) Service Bus connection string and queue/topic name
Method Description
PublishAsync(T message, CancellationToken ct) Serializes the message and sends it to the queue/topic
PublishBatchAsync(IEnumerable<T> messages, CancellationToken ct) Creates a native batch and sends all messages in one operation
DisposeAsync() Disposes the ServiceBusSender and ServiceBusClient

ServiceBusConsumer<T>

Constructor Parameters
ServiceBusConsumer<T>(string connectionString, string queueOrTopicName) Service Bus connection string and queue/topic name
Method Description
StartAsync(Func<T, CancellationToken, Task> handler, CancellationToken ct) Starts the Service Bus processor
StopAsync(CancellationToken ct) Stops the processor gracefully
DisposeAsync() Disposes the ServiceBusProcessor and ServiceBusClient

Requirements

  • .NET 9.0+
  • Azure.Messaging.ServiceBus (installed automatically)

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
3.0.2 109 4/5/2026
3.0.1 102 4/3/2026
2.0.4 103 3/21/2026
2.0.3 94 3/21/2026
2.0.2 99 3/21/2026
2.0.1 99 3/19/2026
1.0.2 251 12/19/2025
1.0.1 271 12/19/2025
1.0.0 271 12/19/2025