Nera.Lib.Messaging.Abstractions
1.0.1
See the version list below for details.
dotnet add package Nera.Lib.Messaging.Abstractions --version 1.0.1
NuGet\Install-Package Nera.Lib.Messaging.Abstractions -Version 1.0.1
<PackageReference Include="Nera.Lib.Messaging.Abstractions" Version="1.0.1" />
<PackageVersion Include="Nera.Lib.Messaging.Abstractions" Version="1.0.1" />
<PackageReference Include="Nera.Lib.Messaging.Abstractions" />
paket add Nera.Lib.Messaging.Abstractions --version 1.0.1
#r "nuget: Nera.Lib.Messaging.Abstractions, 1.0.1"
#:package Nera.Lib.Messaging.Abstractions@1.0.1
#addin nuget:?package=Nera.Lib.Messaging.Abstractions&version=1.0.1
#tool nuget:?package=Nera.Lib.Messaging.Abstractions&version=1.0.1
Nera.Lib.Messaging.Abstractions
Messaging abstractions for Nextera applications, providing standardized interfaces and implementations for both inter-service and in-service event communication.
Table of Contents
Overview
Nera.Lib.Messaging.Abstractions
provides a consistent messaging framework for the Nextera microservice architecture. The library:
- Leverages MassTransit and RabbitMQ as message broker for cross-service communication
- Provides in-memory event bus for in-service event handling
- Standardizes event communication patterns across the system
- Simplifies message handling with base classes and automatic registration
Key Components
Cross-Service Communication:
IIntegrationEvent
- Interface for events shared across servicesIntegrationMessageBase
- Base class for integration eventsIEventBus
- Interface for publishing integration eventsConsumerBase<T>
- Base class for consuming integration events
In-Service Communication:
IInServiceEvent
- Interface for events handled within the same serviceInServiceEventBase
- Base class for in-service eventsIInServiceEventBus
- Interface for publishing and handling in-service eventsInServiceEventHandlerBase<T>
- Base class for handling in-service events
Integration Events
Integration events are used for communication between different services.
Creating an Integration Event
using Nera.Lib.Messaging.Abstractions;
namespace YourService.Events;
public record OrderCompletedIntegrationEvent(
Guid OrderId,
string CustomerEmail,
decimal TotalAmount) : IntegrationMessageBase;
Publishing an Integration Event
public class OrderService
{
private readonly IEventBus _eventBus;
private readonly IMessageContextAccessor _contextAccessor;
public OrderService(IEventBus eventBus, IMessageContextAccessor contextAccessor)
{
_eventBus = eventBus;
_contextAccessor = contextAccessor;
}
public async Task CompleteOrderAsync(Order order)
{
// Business logic...
// Create and publish event
var @event = new OrderCompletedIntegrationEvent(
order.Id,
order.CustomerEmail,
order.TotalAmount);
await _eventBus.Publish(@event);
}
}
Consuming an Integration Event
using MassTransit;
using Microsoft.Extensions.Logging;
using Nera.Lib.Messaging.Abstractions;
namespace YourService.Consumers;
public class OrderCompletedIntegrationEventConsumer
: ConsumerBase<OrderCompletedIntegrationEvent>
{
private readonly IEmailService _emailService;
public OrderCompletedIntegrationEventConsumer(
ILogger<OrderCompletedIntegrationEventConsumer> logger,
IEmailService emailService)
: base(logger)
{
_emailService = emailService;
}
public override async Task Consume(
ConsumeContext<OrderCompletedIntegrationEvent> context)
{
LogMessageReceived(context);
try
{
await _emailService.SendOrderConfirmationAsync(
context.Message.CustomerEmail,
context.Message.OrderId,
context.Message.TotalAmount);
LogMessageProcessed(context);
}
catch (Exception ex)
{
LogMessageError(context, ex);
throw;
}
}
}
In-Service Events
In-service events are used for communication between components within the same service.
Creating an In-Service Event
using Nera.Lib.Messaging.Abstractions;
namespace YourService.Events;
public record UserProfileUpdatedEvent(
Guid UserId,
string Username,
string NewEmail) : InServiceEventBase;
Creating an In-Service Event Handler
using Microsoft.Extensions.Logging;
using Nera.Lib.Messaging.Abstractions;
namespace YourService.Handlers;
public class UserProfileUpdatedEventHandler
: InServiceEventHandlerBase<UserProfileUpdatedEvent>
{
private readonly INotificationService _notificationService;
public UserProfileUpdatedEventHandler(
ILogger<UserProfileUpdatedEventHandler> logger,
INotificationService notificationService)
: base(logger)
{
_notificationService = notificationService;
}
protected override async Task HandleInternal(
UserProfileUpdatedEvent @event,
CancellationToken cancellationToken)
{
await _notificationService.NotifyEmailChangeAsync(
@event.UserId,
@event.Username,
@event.NewEmail);
}
}
Publishing an In-Service Event
public class UserService
{
private readonly IInServiceEventBus _eventBus;
public UserService(IInServiceEventBus eventBus)
{
_eventBus = eventBus;
}
public async Task UpdateUserProfileAsync(UpdateProfileRequest request)
{
// Business logic...
// Create and publish in-service event
var @event = new UserProfileUpdatedEvent(
request.UserId,
request.Username,
request.NewEmail);
await _eventBus.PublishEvent(@event);
}
}
Registration
Register Messaging Services
public void ConfigureServices(IServiceCollection services)
{
// Register all messaging services
services.AddMessagingServices();
// Configure service name
services.Configure<MessagingOptions>(options =>
{
options.ServiceName = "your-service-name";
});
}
Advanced Registration Options
// Register without automatic handler discovery
services.AddMessagingServices(registerInternalHandlers: false);
// Register in-service event handlers from specific assembly
services.AddInServiceEventHandlers(typeof(YourClass).Assembly);
Best Practices
Naming Conventions
- Integration Events:
{Entity}{Action}IntegrationEvent
- In-Service Events:
{Entity}{Action}Event
- Consumers:
{EventName}Consumer
- Handlers:
{EventName}Handler
- Integration Events:
Event Design
- Design events to be immutable
- Use C# records for events
- Include only necessary data
- Consider including context information
Error Handling
- Always use proper error handling in consumers and handlers
- Configure retry policies for transient errors
- Consider the consequences of event failure
When to Use In-Service Events
- Decoupling components within a service
- Asynchronous post-processing operations
- Reducing tight coupling between modules
- Performance optimization by moving secondary processing out of the main flow
Troubleshooting
Common issues and their solutions:
"No consumers available for message"
- Check event namespace and naming
- Ensure
AddMessagingServices()
is called - Verify consumer class inheritance
"Connection to RabbitMQ failed"
- Check environment variables:
ENV_RABBIT_MQ_CONNECTION
,ENV_RABBIT_MQ_USERNAME
,ENV_RABBIT_MQ_PASSWORD
- Verify RabbitMQ server is running
- Check environment variables:
"No handlers registered for event"
- Verify handler registration
- Check namespace and type matching
- Ensure handler classes inherit from correct base class
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 was computed. 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. |
-
net9.0
- MassTransit (>= 8.5.2)
- MassTransit.AspNetCore (>= 7.3.1)
- MassTransit.EntityFrameworkCore (>= 8.5.2)
- MassTransit.RabbitMQ (>= 8.5.2)
- Microsoft.AspNetCore.Http (>= 2.3.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.8)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.8)
- Microsoft.Extensions.Hosting (>= 9.0.8)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.8)
- Nera.Lib.Core (>= 1.0.8)
- OpenTelemetry.Extensions.Hosting (>= 1.12.0)
- Serilog.AspNetCore (>= 9.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Nera.Lib.Messaging.Abstractions:
Package | Downloads |
---|---|
Nera.Lib.Database
Database access layer with Entity Framework Core, Repository pattern, Specification pattern, and advanced querying capabilities for Nera applications |
|
Nera.Lib.Messaging.Sender
A robust messaging sender library for Nera applications providing event-driven communication capabilities with logging, retry policies, and template variable substitution |
GitHub repositories
This package is not used by any popular GitHub repositories.