Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Trapov/Infrastructure.Messaging

Repository files navigation

Infrastructure.Messaging

Library for messaging between services.

Builds

Travis
Infrastructure.Messaging Build Status

Installation

Packages Travis
Infrastructure.Messaging NuGet version
Infrastructure.Messaging.DependencyInjection NuGet version
Infrastructure.Messaging.RabbitMQ NuGet version
Infrastructure.Messaging.InMemory NuGet version

Requirements

  • .NETStandard 2.1
  • Microsoft.Extensions.DependencyInjection (>= 3.0.0)
  • Microsoft.Extensions.Logging.Abstractions (>= 3.0.0)
  • System.Text.Json (>= 4.6.0)

Demo

messaging.gif

How to

Register Messaging via .AddMessaging() extension method provided by Infrastructure.Messaging.Extensions.DependencyInjection package.

var rabbitMQUri = configuration.GetSection("RabbitMq")["uri"];

var serviceProvider = 
    new ServiceCollection()
        .AddLogging(lb => lb.AddConsole())
        .AddMessaging(mc =>
            {
                mc.UseRabbitMQ(cf => cf.Uri = new Uri(rabbitMQUri));
                mc.UseJsonPacker(jso => jso.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
            }, 
            sc => sc
                .AddSingleton<IMessageHandler<TestMessage>, TestMessageHandler>()
                .AddSingleton<IMessageHandler<TestMessageWithEventId>, TestMessageHandler>()
        )
    .BuildServiceProvider();

Implement handlers.

public sealed class TestMessageHandler : 
    IMessageHandler<TestMessage>,
    IMessageHandler<TestMessageWithEventId>
{
    private readonly ILogger<TestMessageHandler> _logger;

    public TestMessageHandler(ILogger<TestMessageHandler> logger)
    {
        _logger = logger;
    }

    public async Task Handle(TestMessage message, CancellationToken cancellationToken)
    {
        await Task.Delay(100);
        _logger.LogInformation("Test message was dispached {message}", message.Ping);
        throw new Exception("AASDSD");
    }

    public Task Handle(TestMessageWithEventId message, CancellationToken cancellationToken)
    {
        _logger.LogInformation("TestMessageWithEventId was dispatched. {eventId}, {text}", message.EventId, message.Text);
        return Task.CompletedTask;
    }
}

Use IMessageRouter.Route() to start the routing process and IMessagePublisher.Publish(IMessage message) to publish a message.

public async Task Main()
{
    CancellationTokenSource = new CancellationTokenSource();

	//..
    MessageRouter = serviceProvider.GetRequiredService<IMessageRouter>();
    Publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
	//..

	await Publisher.Publish(new TestMessage { Ping = "A" }, cancellationToken: CancellationTokenSource.Token);
	
	// ..
	await MessageRouter.Route(cancellationToken: CancellationTokenSource.Token);
}

About

Library for messaging between services.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages