microservice.toolkit.messagemediator 0.7.0

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

// Install microservice.toolkit.messagemediator as a Cake Tool
#tool nuget:?package=microservice.toolkit.messagemediator&version=0.7.0                

Message mediator

The library is a work in progress. It is not yet considered production-ready.

Build Release License: MIT Nuget Nuget

An interface to define how cloud microservices interact each other across multi instances, using request-response pattern.

How to install

Package Manager

Install-Package microservice.toolkit.messagemediator -Version 0.7.0

.NET CLI

dotnet add package microservice.toolkit.messagemediator --version 0.7.0

Package Reference

<PackageReference Include="microservice.toolkit.messagemediator" Version="0.7.0" />

Introduction

In Microservice Toolkit, a microservice has a name (or pattern) and returns with the following structure:

{
    "error": 12,
    "payload": {
        ...
    }
}

Where:

  • Error is the core of the error, it has value when an error occurs during service execution.
  • Payload is the output of the service, it has value when the execution goes well.

Only one of the fields can have a value: if "error" has a value, "payload" doesn't have it, and vice versa.

Implementations

Microservice Toolkit provides some implementations of the message mediator interface:

The implementations of message mediator can work in a single instance environment

Single instance

or in a multi instances environment:

Single instance

A bus can be a message broker (RabbitMQ or Azure Service bus).

Every implementation requires a "service provider" (a delegate) to link the name (pattern) to the instance of a service.

Local

<a name="local"></a> To use in a single instance environment or for testing.

No external dependencies needed.

No external service will be query/use.

RabbitMQ

<a name="rabbitmq"></a> RabbitMQ is an open-source and lightweight message broker which supports multiple messaging protocols. It can be deployed in distributed and federated configurations to meet high-scale, high-availability requirements. In addition, it's the most widely deployed message broker, used worldwide at small startups and large enterprises.

To start building RabbitMQ-based microservices, first install the required packages:

Install-Package RabbitMQ.Client -Version 6.2.2

Or:

<PackageReference Include="RabbitMQ.Client" Version="6.2.2" />

Azure Service Bus

<a name="servicebus"></a> To start building Azure Service Bus -based microservices, first install the required packages:

Install-Package Microsoft.Azure.ServiceBus -Version 5.1.3

Or:

<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.1.3"/>

How to use

Service implementation

To implement a service, extend the abstract class "Service<TRequest, TPayload>", where:

  • "TRequest" is the service input (or request)
  • "TPayload" is the service output (or payload of the response)

Example code:

[MicroService(nameof(UserExists))]
public class UserExists : Service<UserExistsRequest, UserExistsResponse>
{
    public async override Task<ServiceResponse<UserExistsResponse>> Run(UserExistsRequest request)
    {
        return this.SuccessfulResponse(new UserExistsResponse
        {
            Exists = "Alice" == request.Username
        });
    }
}

Services registration

To register the services into IoC (using Microsoft Dependency Injection) and mediator, add at startup program ("services" is an instance of IServiceCollection):

using microservice.toolkit.messagemediator.extension;

[...]

// Uses assembly scan to retrieve all the "Service<,>" implementations
var microservices = Assembly.GetAssembly(typeof(UserExists)).GetServices(); 
// or 
// var microservices = typeof(UserExists).GetAssemblyServices();

// Registers all the microservices as singleton
services.AddServices(microservices);
// Or specify the life time
// services.AddServices(ServiceLifetime.Scoped, microservices);

[...]

// Registers the "service factory" 
services.AddSingleton<ServiceFactory>(serviceProvider => 
    serviceProvider.ByNameServiceFactory(microservices)
);

[...]

// Registers the message mediator 
services.AddSingleton<IMessageMediator, LocalMessageMediator>();

Service call

To call a service using a mediator:

// to explicit only payload type:
var response = await mediator.Send<int>(nameof(SquarePow), 2));
// or, to explicit request and payload type:
var response = await mediator.Send<int, int>(nameof(SquarePow), 2));
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 (1)

Showing the top 1 NuGet packages that depend on microservice.toolkit.messagemediator:

Package Downloads
microservice.toolkit.entitystoremanager

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 119 7/30/2024
2.0.0 80 7/28/2024
1.1.1 139 7/19/2024
1.1.0 135 7/19/2024
1.0.0 137 6/10/2024
0.11.2 180 4/6/2024
0.11.1 189 3/30/2024
0.11.0 130 3/30/2024
0.10.2 306 3/15/2023
0.10.1 368 1/18/2023
0.10.0 347 1/11/2023
0.9.1 345 11/22/2022
0.9.0 385 11/16/2022
0.8.0 475 10/12/2022
0.7.0 470 4/28/2022
0.6.2 470 2/28/2022
0.6.1 479 2/21/2022
0.6.0 527 1/15/2022
0.5.0 359 12/8/2021
0.4.9 322 12/2/2021
0.4.8 296 12/2/2021
0.4.7 313 12/2/2021
0.4.6 321 12/1/2021
0.4.5 3,477 11/25/2021
0.4.4 375 11/2/2021
0.4.3 356 11/2/2021
0.4.2 384 10/27/2021
0.4.1 417 10/21/2021
0.4.0 526 10/3/2021