MVFC.Messaging.Azure
2.0.3
See the version list below for details.
dotnet add package MVFC.Messaging.Azure --version 2.0.3
NuGet\Install-Package MVFC.Messaging.Azure -Version 2.0.3
<PackageReference Include="MVFC.Messaging.Azure" Version="2.0.3" />
<PackageVersion Include="MVFC.Messaging.Azure" Version="2.0.3" />
<PackageReference Include="MVFC.Messaging.Azure" />
paket add MVFC.Messaging.Azure --version 2.0.3
#r "nuget: MVFC.Messaging.Azure, 2.0.3"
#:package MVFC.Messaging.Azure@2.0.3
#addin nuget:?package=MVFC.Messaging.Azure&version=2.0.3
#tool nuget:?package=MVFC.Messaging.Azure&version=2.0.3
MVFC.Messaging.Azure
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 |
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
ServiceBusProcessorevent-driven model — no manual polling needed. AutoCompleteMessagesis 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). StartAsyncstarts the processor;StopAsyncstops 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
| Product | Versions 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. |
-
net10.0
- Azure.Messaging.ServiceBus (>= 7.20.1)
- MVFC.Messaging.Core (>= 2.0.3)
-
net9.0
- Azure.Messaging.ServiceBus (>= 7.20.1)
- MVFC.Messaging.Core (>= 2.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.