It returns the same instance because you're resolving the instance from the container. A container is an implicit lifetime scope.
If you want different instances IN different lifetime scopes, you have to start a new lifetime scope.
var builder = new ContainerBuilder();
builder.RegisterType<MyType>().InstancePerLifetimeScope();
var container = builder.Build();
using(var scope = container.BeginLifetimeScope())
{
// Resolve from a SCOPE not the CONTAINER
var obj = scope.Resolve<MyType>();
}
If you want a different object every time you ask for one, you can specify InstancePerDependency() or just leave off the lifetime specifier entirely.
builder.RegisterType<MyType>();
// same as
builder.RegisterType<MyType>().InstancePerDependency();
Then when you resolve, you get a new one every time, regardless of the scope.
-T
On Wednesday, January 30, 2013 2:50:40 PM UTC-8,
wallac...@gmail.com wrote:
I'm migrating some things to InstancePerLifetimeScope, this is working great for things that I can migrate.
However consider this example:
![]()
Resolve returns the same object for service1 and service2.
Is there an option to return different objects when NOT under a
lifetime scope?
Cheers and regards
Wallace