0

With some presumptions from how AutoFac works , i couldn't get my code working in my ASP.NET 5 app.I have integrated AutoFac as DI framework into my ASP.NET 5 app and everything works fine, and dependencies which i register before first call to Build method gets injected fine but when i try to register Modules from other assemblies referenced by main assembly i can't inject theme in controller anymore.

this is what i have tried:

ContainerBuilder builder = new ContainerBuilder();
   builder.Register<MyService>().As<IMyService>();     
 builder.Build();

and after that i've create and registered a Module :

public class MyModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<SomeService>()
                .As<ISomeService>()
                .SingleInstance();
        }
    }

and within a manager class by injecting ILifetimeScope :

 public class ManagerClass(){
     private readonly ILifetimeScope _lifetimeScope;

     public ManagerClass(ILifetimeScope lifetimeScope){
          _lifetimeScope = lifetimeScope;
     }

     public void RegisterModules(){
            _lifetimeScope.BeginLifetimeScope(builder =>
            {   
                   builder.RegisterAssemblyModules(blueprint.Assemblies.Select(b=>b.Assembly).ToArray());
            });
     }
 } 

i put a breakpoint on Load method of MyModule class and it gets hit, but when i try to inject ISomeService (registered by MyModule) i get ComponentNotRegisteredException exception.

Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63

1 Answers1

1

This isn't normally how dependency injection is configured. You should configure your dependencies once on startup. Maybe a factory pattern is a better solution in this case?

If you really want that to work though, you will need a reference to the current IContainer and update it

Community
  • 1
  • 1
Dealdiane
  • 3,984
  • 1
  • 24
  • 35