I'm try to supply a different service to some tagged lifetime scopes in AutoFac, but can't seem to get the hang of it.
I've tried using the custom lifetime from Instance per matching lifetime scope, with default? but that didn't work.
I've written a test that illustrates what I'm trying to do:
[TestMethod]
public void NamedLifetimeTests()
{
var builder = new ContainerBuilder();
builder.Register(c => new ChildA()).As<Parent>().InstancePerLifetimeScope();
builder.Register(c => new ChildB()).As<Parent>().InstancePerMatchingLifetimeScope("system").PreserveExistingDefaults();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
var parent = scope.Resolve<Parent>();
Assert.IsTrue(parent.GetType() == typeof(ChildA));
}
using (var scope = container.BeginLifetimeScope("system"))
{
var parent = scope.Resolve<Parent>();
Assert.IsTrue(parent.GetType() == typeof(ChildB));
}
}
Is this possible?