I have a classlib which is used by Console app and Web API. I am using Autofac module to register the dependencies as follows:
public class TenantAutofacModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerTenant();
}
}
The lifecycle above InstancePerTenant
works for Web API and does not work for Console app because the lifetime scope requires HTTP request.
I am wondering in the TenantAutofacModule
, if there is a way to know who is the caller so that I could register the lifecycle either with InstancePerTenant
or InstancePerLifetimeScope
The lifecycle above InstancePerTenant works for Web API and does not work for Console app because the lifetime scope requires HTTP request.
InstancePerTenant
doesn't require a HTTP request. It depends on your ITenantIdentificationStrategy
implementation.
When you configure your container, you can choose a different implementation based on configuration
if(config.useConsole) {
builder.RegisterType<ConsoleTenantResolverStrategy>()
.As<ITenantIdentificationStrategy>()
.SingleInstance();
} else {
builder.RegisterType<WebTenantResolverStrategy>()
.As<ITenantIdentificationStrategy>()
.SingleInstance();
}
and to get your own config
you can read Configuration in ASP.NET Core which will give you the guidance for .net core