microservice.toolkit.messagemediator 0.6.2

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

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

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.6.2

.NET CLI

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

Package Reference

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

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:

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(MyService)).GetServices(); 
// or 
var microservices = typeof(MyService).GetAssemblyServices();

// Registers all the microservices
services.AddServices(microservices);

// Registers the "service factory" to resolve pattern into a service instance
services.AddSingleton<ServiceFactory>(serviceProvider => pattern =>
    serviceProvider.GetService(microservices.First(ms => ms.Name.Equals(pattern))) as IService
);

// 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 121 7/30/2024
2.0.0 82 7/28/2024
1.1.1 141 7/19/2024
1.1.0 137 7/19/2024
1.0.0 139 6/10/2024
0.11.2 182 4/6/2024
0.11.1 191 3/30/2024
0.11.0 132 3/30/2024
0.10.2 308 3/15/2023
0.10.1 370 1/18/2023
0.10.0 349 1/11/2023
0.9.1 347 11/22/2022
0.9.0 387 11/16/2022
0.8.0 477 10/12/2022
0.7.0 472 4/28/2022
0.6.2 472 2/28/2022
0.6.1 481 2/21/2022
0.6.0 529 1/15/2022
0.5.0 361 12/8/2021
0.4.9 324 12/2/2021
0.4.8 298 12/2/2021
0.4.7 315 12/2/2021
0.4.6 323 12/1/2021
0.4.5 3,479 11/25/2021
0.4.4 377 11/2/2021
0.4.3 358 11/2/2021
0.4.2 386 10/27/2021
0.4.1 419 10/21/2021
0.4.0 528 10/3/2021