.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
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