You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.AddClasses(classes => classes.AssignableTo<ISingletonService>().WithoutAttribute<NotInjectAttribute>())
.AsImplementedInterfaces(x => x != typeof(ISingletonService))
.AsSelf()
.WithSingletonLifetime() // A
.AddClasses(classes => classes.AssignableTo<ISingletonService>().WithoutAttribute<NotInjectAttribute>())
.AsSelfWithInterfaces()
.WithSingletonLifetime() //B
public class TestService2 : ISingletonService
{
public TestService2()
{
Console.WriteLine("ctor2 in TestService ");
}
}
Console.WriteLine(provider.GetService<ISingletonService>().GetType().FullName);
Console.WriteLine(provider.GetService<TestService2>().GetType().FullName);
beacause AsSelfWithInterfaces can not filter interface, so I seprate AsImplementedInterfaces+AsSelf
but code will print 2 times "ctor2 in TestService " in scense A , print 1 time in scense B
The text was updated successfully, but these errors were encountered:
Yeah, AsSelfWithInterfaces is kinda like AsImplementedInterfaces + AsSelf, but there's a small difference, which is why you're seeing two outputs in the first scenario and only one in the second scenario. When using AsSelfWithInterfaces, it will register as itself once and base all the interface registrations on the self-registration. That means that if you're using a singleton lifetime, you will only have a true singleton when using AsSelfWithInterfaces, while AsImplementedInterfaces will give you a singleton per interface.
I've added an overload with a predicate in #209 (see 1a99cfb)
beacause AsSelfWithInterfaces can not filter interface, so I seprate AsImplementedInterfaces+AsSelf
but code will print 2 times "ctor2 in TestService " in scense A , print 1 time in scense B
The text was updated successfully, but these errors were encountered: