Skip to content

Library for actor identification through services in MassTransit.

License

Notifications You must be signed in to change notification settings

qoollo/masstransit-identity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MassTransit Identity

Library for actor identification through services in MassTransit. Repository also contains simple example projects with providers and consumers.

Usage steps:

1. Add nessesary services to service collection in your project

1.1 Add Identity Token to service collection

Put this code in your ConfigureServices method:

services.AddScoped<MassTransitIdentityToken>();

1.2 Add filter configurations to your MassTransit transport configuration

Add these lines:

cfg.UsePublishFilter(typeof(IdentityTokenPublishFilter<>), context);
cfg.UseSendFilter(typeof(IdentityTokenSendFilter<>), context);
cfg.UseConsumeFilter(typeof(IdentityTokenConsumeFilter<>), context);

WARNING: It is strongly recommended to add all filters to configuration, no matter for what purposes you are going to use Identity Token: receiving, sending/publishing or redirecting.

2. Use token where you need to

2.1 Put token as dependency in service constructor

public class ScopedService
{
    private readonly MassTransitIdentityToken _token;
    private readonly ILogger<ScopedService> _logger;
    private readonly IRequestClient<SomeMassTransitRequest> _requestClient;

    public ScopedService(MassTransitIdentityToken token, 
            ILogger<ScopedService> logger,
            IRequestClient<MassTransitContract> requestClient)
    {
        _token = token;
        _logger = logger;
        _requestClient = requestClient;
    }
}

2.2 Create identity token for your scope

Put token into Value property of MassTransitIdentityToken instance. After this you can call services, which use identity token. What is more important, now you can send and publish messages to other MassTransit actors like consumers.

public class ScopedService
{
    ...

    public void Foo(Guid clientId)
    {
        _token.Value = clientId;

        var response = _requestClient.GetResponse<SomeMassTansitResponse>(new { });
    }
}

TIP: As you can see, identity token value type is Guid.

2.3 Get identity token in other services

public class AnotherServiceConsumer : IConsumer<SomeMassTransitRequest>
{
    private readonly MassTransitIdentityToken _token;
    private readonly ILogger<AnotherServiceConsumer> _logger;

    public SomeConsumer(MassTransitIdentityToken token, 
        ILogger<AnotherServiceConsumer> logger)
    {
        _token = token;
        _logger = logger;
    }

    public async Task Consume(ConsumeContext<SomeMassTransitRequest> context)
    {
        _logger.LogInformation($"{typeof(AnotherServiceConsumer)}: Consumed token {_token.Value}");

        await context.RespondAsync<SomeMassTansitResponse>(new { });
    }
}

TIP: Identity token secured from rewriting.

About

Library for actor identification through services in MassTransit.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages