autofac / Autofac

An addictive .NET IoC container
https://autofac.org
MIT License
4.44k stars 836 forks source link

Startable Singleton instantiated twice #1409

Closed jvmlet closed 5 months ago

jvmlet commented 5 months ago

Startable singleton instantiated twice

Steps to Reproduce

public class MyClass : IStartable{
 public MyClass (){
    // gets called twice
  }
  public void Start(){
  } 
}
public class MyModule: Module {
    protected override void Load(ContainerBuilder builder) {
        builder
            .RegisterType<MyClass>()
            .AsSelf()
            .SingleInstance();
    }
}
new ContainerBuilder()
.RegisterModule<MyModule>()
.Build()
.Resolve<MyClass>();

Expected Behavior

MyClass constructor is invoked once. Another issue : If I add As<IStartable>() to the registration, Start is invoked twice...

Dependency Versions

7.0.0

jvmlet commented 5 months ago

Sorry, my fault, was registering it twice.