Nera.Lib.Messaging.Abstractions 1.0.1

There is a newer version of this package available.
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
                    
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="Nera.Lib.Messaging.Abstractions" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nera.Lib.Messaging.Abstractions" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Nera.Lib.Messaging.Abstractions" />
                    
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 Nera.Lib.Messaging.Abstractions --version 1.0.1
                    
#r "nuget: Nera.Lib.Messaging.Abstractions, 1.0.1"
                    
#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 Nera.Lib.Messaging.Abstractions@1.0.1
                    
#: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=Nera.Lib.Messaging.Abstractions&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Nera.Lib.Messaging.Abstractions&version=1.0.1
                    
Install as a Cake Tool

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

  1. Cross-Service Communication:

    • IIntegrationEvent - Interface for events shared across services
    • IntegrationMessageBase - Base class for integration events
    • IEventBus - Interface for publishing integration events
    • ConsumerBase<T> - Base class for consuming integration events
  2. In-Service Communication:

    • IInServiceEvent - Interface for events handled within the same service
    • InServiceEventBase - Base class for in-service events
    • IInServiceEventBus - Interface for publishing and handling in-service events
    • InServiceEventHandlerBase<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

  1. Naming Conventions

    • Integration Events: {Entity}{Action}IntegrationEvent
    • In-Service Events: {Entity}{Action}Event
    • Consumers: {EventName}Consumer
    • Handlers: {EventName}Handler
  2. Event Design

    • Design events to be immutable
    • Use C# records for events
    • Include only necessary data
    • Consider including context information
  3. Error Handling

    • Always use proper error handling in consumers and handlers
    • Configure retry policies for transient errors
    • Consider the consequences of event failure
  4. 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:

  1. "No consumers available for message"

    • Check event namespace and naming
    • Ensure AddMessagingServices() is called
    • Verify consumer class inheritance
  2. "Connection to RabbitMQ failed"

    • Check environment variables: ENV_RABBIT_MQ_CONNECTION, ENV_RABBIT_MQ_USERNAME, ENV_RABBIT_MQ_PASSWORD
    • Verify RabbitMQ server is running
  3. "No handlers registered for event"

    • Verify handler registration
    • Check namespace and type matching
    • Ensure handler classes inherit from correct base class
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.0.2 94 9/28/2025
1.0.1 103 9/27/2025
1.0.0 306 9/9/2025