My goal is to serve an interface implementation depending on the scope.
I figured to solve that with the following code, but this results in A being overwritten by B
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<ImplA>().As<IMyInterface>().InstancePerMatchingLifetimeScope(MyScope.A);
containerBuilder.RegisterType<ImplB>().As<IMyInterface>().InstancePerMatchingLifetimeScope(MyScope.B);
IContainer container = containerBuilder.Build();
using (ILifetimeScope lifetimeScope = container.BeginLifetimeScope(MyScope.A))
{
IMyInterface c = lifetimeScope.Resolve<IMyInterface>();
Console.WriteLine(c.GetType());
}
using (ILifetimeScope lifetimeScope = container.BeginLifetimeScope(MyScope.B))
{
var c = lifetimeScope.Resolve<IMyInterface>();
Console.WriteLine(c.GetType());
}
I know I can register Types within a lifetimescope in the following way, but I want to configure this from within a Module
using (ILifetimeScope lifetimeScope = container.BeginLifetimeScope(MyScope.A, builder =>
{
containerBuilder.RegisterType<ImplA>().As<IMyInterface>();
}))
Is there a way to solve this at Module level?