MVFC.Messaging.Core
3.0.2
dotnet add package MVFC.Messaging.Core --version 3.0.2
NuGet\Install-Package MVFC.Messaging.Core -Version 3.0.2
<PackageReference Include="MVFC.Messaging.Core" Version="3.0.2" />
<PackageVersion Include="MVFC.Messaging.Core" Version="3.0.2" />
<PackageReference Include="MVFC.Messaging.Core" />
paket add MVFC.Messaging.Core --version 3.0.2
#r "nuget: MVFC.Messaging.Core, 3.0.2"
#:package MVFC.Messaging.Core@3.0.2
#addin nuget:?package=MVFC.Messaging.Core&version=3.0.2
#tool nuget:?package=MVFC.Messaging.Core&version=3.0.2
MVFC.Messaging.Core
π§π· Leia em PortuguΓͺs
The foundation layer for the MVFC.Messaging ecosystem. This package provides the interfaces and abstract base classes that every messaging provider implements. You typically do not use this package directly β instead, you install it alongside a provider package (AWS, Azure, RabbitMQ, etc.) that adds the concrete broker integration.
Package
| Package | Downloads |
|---|---|
| MVFC.Messaging.Core |
Installation
dotnet add package MVFC.Messaging.Core
Core Interfaces
IMessagePublisher<T>
Defines the contract for publishing messages to a broker. All provider publishers implement this interface.
public interface IMessagePublisher<in T>
{
/// <summary>
/// Publishes a single message to the broker.
/// </summary>
Task PublishAsync(T message, CancellationToken cancellationToken = default);
/// <summary>
/// Publishes multiple messages to the broker in a single operation.
/// </summary>
Task PublishBatchAsync(IEnumerable<T> messages, CancellationToken cancellationToken = default);
}
Key points:
- The generic type
Tis contravariant (in), so aIMessagePublisher<object>can accept any message type. - Both methods support
CancellationTokenfor cooperative cancellation. - Batch publishing semantics vary by provider (some use native batch APIs, others publish sequentially).
IMessageConsumer<T>
Defines the contract for consuming messages from a broker. All provider consumers implement this interface.
public interface IMessageConsumer<T>
{
/// <summary>
/// Starts consuming messages. Each received message is passed to the handler delegate.
/// </summary>
Task StartAsync(Func<T, CancellationToken, Task> messageHandler, CancellationToken cancellationToken = default);
/// <summary>
/// Stops consuming messages gracefully.
/// </summary>
Task StopAsync(CancellationToken cancellationToken = default);
}
Key points:
- The
messageHandlerdelegate receives each message along with aCancellationToken. StartAsyncis non-blocking β it starts a background consumer loop and returns immediately.StopAsyncsignals the consumer loop to stop and waits for pending work to complete.
Base Classes
MessagePublisherBase<T>
Abstract base class that implements IMessagePublisher<T> with built-in argument validation. Provider publishers inherit from this class and override only the internal methods:
public abstract class MessagePublisherBase<T> : IMessagePublisher<T>
{
// Override these in your provider:
protected abstract Task PublishInternalAsync(T message, CancellationToken cancellationToken);
protected abstract Task PublishBatchInternalAsync(IEnumerable<T> messages, CancellationToken cancellationToken);
// Public API β validates arguments, then delegates to the internal methods:
public async Task PublishAsync(T message, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(message);
await PublishInternalAsync(message, cancellationToken).ConfigureAwait(false);
}
public async Task PublishBatchAsync(IEnumerable<T> messages, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(messages);
await PublishBatchInternalAsync(messages, cancellationToken).ConfigureAwait(false);
}
}
MessageConsumerBase<T>
Abstract base class that implements IMessageConsumer<T> with handler storage and argument validation. Provider consumers inherit from this class:
public abstract class MessageConsumerBase<T> : IMessageConsumer<T>
{
// The stored handler delegate β available to subclasses via the protected property.
protected Func<T, CancellationToken, Task>? Handler { get; private set; }
// Override these in your provider:
protected abstract Task StartInternalAsync(CancellationToken cancellationToken);
protected abstract Task StopInternalAsync(CancellationToken cancellationToken);
// Public API β stores the handler, then starts the consumer loop:
public async Task StartAsync(Func<T, CancellationToken, Task> messageHandler, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(messageHandler);
Handler = messageHandler;
await StartInternalAsync(cancellationToken).ConfigureAwait(false);
}
public async Task StopAsync(CancellationToken cancellationToken = default) =>
await StopInternalAsync(cancellationToken).ConfigureAwait(false);
}
Creating a Custom Provider
To create your own messaging provider, you need to:
- Inherit from
MessagePublisherBase<T>and implementPublishInternalAsyncandPublishBatchInternalAsync. - Inherit from
MessageConsumerBase<T>and implementStartInternalAsyncandStopInternalAsync. - Implement
IAsyncDisposableon both classes to properly release broker connections.
public sealed class MyBrokerPublisher<T>(MyBrokerClient client, string destination)
: MessagePublisherBase<T>, IAsyncDisposable
{
protected override async Task PublishInternalAsync(T message, CancellationToken cancellationToken)
{
var json = JsonSerializer.Serialize(message);
await client.SendAsync(destination, json, cancellationToken);
}
protected override async Task PublishBatchInternalAsync(
IEnumerable<T> messages, CancellationToken cancellationToken)
{
foreach (var message in messages)
await PublishInternalAsync(message, cancellationToken);
}
public ValueTask DisposeAsync()
{
client?.Dispose();
return ValueTask.CompletedTask;
}
}
Compatible Providers
- MVFC.Messaging.AWS β Amazon SQS
- MVFC.Messaging.Azure β Azure Service Bus
- MVFC.Messaging.Confluent β Apache Kafka (Confluent)
- MVFC.Messaging.GCP β Google Cloud Pub/Sub
- MVFC.Messaging.InMemory β In-Memory (for testing)
- MVFC.Messaging.Nats.IO β NATS.io
- MVFC.Messaging.RabbitMQ β RabbitMQ
- MVFC.Messaging.StackExchange β Redis Streams
Requirements
- .NET 9.0+
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
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (8)
Showing the top 5 NuGet packages that depend on MVFC.Messaging.Core:
| Package | Downloads |
|---|---|
|
MVFC.Messaging.StackExchange
Biblioteca .NET para integração com Redis Streams via StackExchange.Redis, fornecendo abstrações para publicação e consumo de mensagens em streams de forma eficiente e escalável. |
|
|
MVFC.Messaging.Confluent
Biblioteca .NET para integração com Apache Kafka via Confluent.Kafka, fornecendo abstrações para publicação e consumo de mensagens em tópicos de forma eficiente e escalável. |
|
|
MVFC.Messaging.InMemory
Biblioteca .NET para mensageria in-memory, ideal para cenários de testes, desenvolvimento e simulação de filas e tópicos sem dependências externas. |
|
|
MVFC.Messaging.GCP
Biblioteca .NET para integração com Google Cloud Pub/Sub, fornecendo abstrações para publicação e consumo de mensagens em tópicos de forma eficiente e escalável. |
|
|
MVFC.Messaging.Azure
Biblioteca .NET para integração com Azure Service Bus, fornecendo abstrações para publicação e consumo de mensagens em filas e tópicos de forma simples e eficiente. |
GitHub repositories
This package is not used by any popular GitHub repositories.