Resolve dependencies in the MultiTenantConnectionBuilder method

Hi, I’d need to resolve the connection string to be used by outbox feature. We need to provide support for multi-tenant system that provides the client tokens in every incoming message. All the logic to retrieve a client’s connection string is in a service called IClientConnectionService. Is it possible to inject this service in the buildConnectionFromTenantData callback?

Link: Multi-tenant support • Sql Persistence • Particular Docs

var persistence = endpointConfiguration.UsePersistence();
persistence.MultiTenantConnectionBuilder(tenantIdHeaderName: “TenantHeaderName”,
buildConnectionFromTenantData: tenantId =>
{
resolve IClientConnectionService service;
var connection = service.Get(tenantId);
return new SqlConnection(connection);
});

Is the IClientConnectionService service used by any of the message handlers? Why does it need to be in the NServiceBus container?

You could create an instance of the service outside of NServiceBus and call it directly from inside the factory method.

i.e.

var service = new ClientConnectionService<SqlPersistence>();

var persistence = endpointConfiguration.UsePersistence();
persistence.MultiTenantConnectionBuilder(
  tenantIdHeaderName: "TenantHeaderName",
  buildConnectionFromTenantData: tenantId => new SqlConnection(service.Get(tenantId))
);