2

I know that you can add registrations to a LifetimeScope when it is created like this:

using(var scope = container.BeginLifetimeScope(builder =>  
    {  builder.RegisterType<Override>().As<IService>();
       builder.RegisterModule<MyModule>();
    }))
{
  // The additional registrations will be available
  // only in this lifetime scope.
}

Is it possible to add registrations after the LifetimeScope is created? (to add registrations inside the using block for example)

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
dmorganb
  • 1,408
  • 16
  • 26

1 Answers1

0

You can access the ComponentRegistry property of the ILifetimeScope, with this object you can register new IComponentRegistration which you can create using RegistrationBuilder static method :

using(var scope = container.BeginLifetimeScope())
{
    var registration = RegistrationBuilder.ForType<ServiceA>()
                                          .As<IService>()
                                          .CreateRegistration();
    scope.ComponentRegistry.Register(registration); 

    scope.Resolve<IService>(); 
}
Cyril Durand
  • 15,834
  • 5
  • 54
  • 62