DotNetBrightener.Plugins.EventPubSub.MassTransit 2024.0.14.6-preview-2381603

This is a prerelease version of DotNetBrightener.Plugins.EventPubSub.MassTransit.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package DotNetBrightener.Plugins.EventPubSub.MassTransit --version 2024.0.14.6-preview-2381603                
NuGet\Install-Package DotNetBrightener.Plugins.EventPubSub.MassTransit -Version 2024.0.14.6-preview-2381603                
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="DotNetBrightener.Plugins.EventPubSub.MassTransit" Version="2024.0.14.6-preview-2381603" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetBrightener.Plugins.EventPubSub.MassTransit --version 2024.0.14.6-preview-2381603                
#r "nuget: DotNetBrightener.Plugins.EventPubSub.MassTransit, 2024.0.14.6-preview-2381603"                
#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.
// Install DotNetBrightener.Plugins.EventPubSub.MassTransit as a Cake Addin
#addin nuget:?package=DotNetBrightener.Plugins.EventPubSub.MassTransit&version=2024.0.14.6-preview-2381603&prerelease

// Install DotNetBrightener.Plugins.EventPubSub.MassTransit as a Cake Tool
#tool nuget:?package=DotNetBrightener.Plugins.EventPubSub.MassTransit&version=2024.0.14.6-preview-2381603&prerelease                

MassTransit Integration for Event PubSub Library

© 2024 DotNet Brightener

Versions

Library Version
EventPubSub Core NuGet Version
MassTransit Integration Library NuGet Version
Dependency Injection Library NuGet Version
Azure Service Bus with MassTransit Library NuGet Version
RabbitMq with MassTransit Library NuGet Version

Installation

dotnet package add DotNetBrightener.Plugins.EventPubSub
dotnet package add DotNetBrightener.Plugins.EventPubSub.DependencyInjection
dotnet package add DotNetBrightener.Plugins.EventPubSub.MassTransit
dotnet package add DotNetBrightener.Plugins.EventPubSub.MassTransit.AzureServiceBus
# or 
dotnet add package DotNetBrightener.Plugins.EventPubSub.MassTransit.RabbitMq

Usage

Configuration

Option 1: Use IConfiguration to configure the service bus

// Initialize EventPubSubService
var eventPubSubConfig = builder.Services
                               .AddEventPubSubService()
                                // scan for event messages in the given assembly
                               .AddEventMessagesFromAssemblies(Assembly.GetExecutingAssembly(), typeof(DistributedTestMessage).Assembly)
                                // scan for event handlers in the given assembly
                               .AddEventHandlersFromAssemblies(Assembly.GetExecutingAssembly());

var massTransitConfigurator = eventPubSubConfig.EnableMassTransit();

// Add Azure Service Bus
massTransitConfigurator.UseAzureServiceBus(builder.Configuration);

// Or Add RabbitMq - Note that only one or the other can be used in an application.
// massTransitConfigurator.UseRabbitMq(builder.Configuration);

// this should be called before builder.Build(), or before exiting ConfigureServices method
massTransitConfigurator.Finalize();

In your appsettings.json file, add the the following configuration:

  "ServiceBusConfiguration": {
    "ConnectionString": "<your-endpoint-to-azure-service-bus>",
    "SubscriptionName": "<application-name>",
    "IncludeNamespaceForTopicName": false
  },
  "RabbitMqConfiguration": {
    "Host": "<rabbit-mq-host>",
    "Username": "<rabbit-mq-username>",
    "Password": "<rabbit-mq-password>",
    "VirtualHost": "<rabbit-mq-virtual-host>",
    "SubscriptionName": "<application-name>",
    "IncludeNamespaceForTopicName": false
  }
Option 2: Configure from code

// Initialize EventPubSubService
var eventPubSubConfig = builder.Services
                               .AddEventPubSubService()
                                // scan for event messages in the given assembly
                               .AddEventMessagesFromAssemblies(Assembly.GetExecutingAssembly(), typeof(DistributedTestMessage).Assembly)
                                // scan for event handlers in the given assembly
                               .AddEventHandlersFromAssemblies(Assembly.GetExecutingAssembly());

var massTransitConfigurator = eventPubSubConfig.EnableMassTransit();

massTransitConfigurator.SetSubscriptionName("<application-name>");
// if you want to exclude namespace in entity name
massTransitConfigurator.ExcludeNamespaceInEntityName();

// Add Azure Service Bus
massTransitConfigurator.UseAzureServiceBus("<azure-connection-string>");

// Or Add RabbitMq - Note that only one or the other can be used
/*
massTransitConfigurator.UseRabbitMq("<rabbit-mq-host>",
                                    "<rabbit-mq-username>",
                                    "<rabbit-mq-password>",
                                    "<rabbit-mq-virtual-host>");
*/

// this should be called before builder.Build(), or before exiting ConfigureServices method
massTransitConfigurator.Finalize();

Event Message Definition

Create a class that represents the event message. The class must implement the IDistributedEventMessage interface.


namespace YourProject.NameSpace;

public class DistributedTestMessage : IDistributedEventMessage
{
    public string Name { get; set; }

    // more payload properties
}

Event Handler Definition

Create a class derived from DistributedEventEventHandler<YourEventMessage> and override HandleEvent method to handle the event message.

With this implementation, you'll be able to access the original payload of the messsage by accessing OriginPayload property.



public class TestEventHandlerDistributed(ILogger<TestEventHandlerDistributed> logger)
    : DistributedEventEventHandler<DistributedTestMessage>
{
    private readonly ILogger _logger = logger;
    
    public override Task<bool> HandleEvent(DistributedTestMessage eventMessage)
    {
        // access the `OriginPayload` 
        var origin = OriginPayload;

        _logger.LogInformation($"Received message: {eventMessage.Name}");

        return Task.FromResult<bool>(true);
    }
}

Request - Response Message Pattern

Create 2 (two) classes that represents the request message and the response message. The request message class must implement the IRequestMessage interface, while the response message class must implement the IResponseMessage<T> interface, where T is the request message type.


namespace YourProject.NameSpace;

public class RequestMessage : IRequestMessage
{
    public string Name { get; set; }

    // more payload properties
}

public class ResponseMessage : IResponseMessage<RequestMessage>
{
    public string Name { get; set; }

    // more payload properties
}

When you want to initiate a request to obtain the needed response, use IEventPublisher to initiate it as follow:


var requestMessage = new RequestMessage
{
    Name = eventMessage.Name
};

var response = await eventPublisher.GetResponse<ResponseMessage, RequestMessage>(requestMessage);

Request - Response Handler Definition

Create a class derived from RequestResponder<YourRequestMessage> and override HandleEvent method to handle the event message.



public class HandleRequestMessage(ILogger<HandleRequestMessage> logger)
    : RequestResponder<RequestMessage>
{    
    public override async Task<bool> HandleEvent(RequestMessage eventMessage)
    {
        // do something with the request message, e.g. logging it
        logger.LogInformation($"Received message: {eventMessage.Name}");

        // create a response message - this response message must implement IResponseMessage<T> interface
        var responseMessage = new ResponseMessage
        {
            Name = eventMessage.Name
        };

        // send the response message
        await SendResponse(responseMessage);

        return Task.FromResult<bool>(true);
    }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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 DotNetBrightener.Plugins.EventPubSub.MassTransit:

Package Downloads
DotNetBrightener.Plugins.EventPubSub.MassTransit.AzureServiceBus

Package Description

DotNetBrightener.Plugins.EventPubSub.MassTransit.RabbitMq

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2024.0.14.6-preview-2390101 78 8/26/2024
2024.0.14.6-preview-2381603 74 8/25/2024