Paulo Oliveira
Paulo Oliveira

Reputation: 21

MassTransit integration with SaasKit: How to generate the tenant when consuming messages?

I'm integrating MassTransit on a solution that uses SaasKit to implement multi-tenancy.

My DbContext uses the method of defining the connection string in "OnConfiguring" as described here:

https://benfoster.io/blog/aspnet-core-multi-tenancy-data-isolation-with-entity-framework

The problem is that when consuming messages, the Tenant does not exists, so the DbContext is not properly initialized.

How do I inject the Tenant inside the lifecycle of MassTransit?

I'm using AspNet Core dependency injection, where I add the tenant like this:

public void Configure(...)
(...)
app.UseMultitenancy<MyTenant>();

Bus configuration:

var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
(...)
cfg.ReceiveEndpoint(host, "customer-updated", ep =>
                {
                    ep.PrefetchCount = 1;
                    ep.UseMessageRetry(r => r.Interval(2, 100));

                    ep.ConfigureConsumer<CustomerUpdatedConsumer>(provider);
                    EndpointConvention.Map<ICustomerUpdated>(ep.InputAddress);
                });

Inside my DbContext constructor, only when consuming messages, I keep getting MyTenant as null ...

 public MyDbContext(DbContextOptions<MyDbContext> options, MyTenant tenant)
        {
            this.tenant = tenant;
        }

Does anyone already tried to integrate SaasKit with MassTransit?

Thanks

Upvotes: 1

Views: 316

Answers (0)

Related Questions