I'm trying to implement component root by creating new containers for each project in the execution stack. For example, in the service layer, I create a container that has the service layer and the facade layer interfaces.
Once I'm in the facade layer, I then create a new container that includes the facade and business layer interfaces.
I also have a shared layer that contains certain registrations, e.g. a logger that is used by all layers to add logging information as the code is executed throughout the different layers.
I have a class in the business layer:
public class Validator : IValidator
{
ILogger _logger;
public Validator(ILogger logger)
{
_logger = logger;
}
}
The Logger is in the shared container.
But when I try to resolve the Validator using the following:
var validator = _ioc.Container.Resolve<IValidator>();
validator.Validate();
I get the following exception:
Autofac.Core.DependencyResolutionException occurred
HResult=0x80131500
Message=An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Validator2 (ReflectionActivator), Services = [BusinessLayer.Validators.IValidator], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope
Source=Autofac
StackTrace:
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Container.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
at BusinessLayer.CompRootBusinessProduct.GetProduct(Int32 id) in C:\Users\Dinua\documents\visual studio 2017\Projects\IoCAutofac\IocAutofac\BusinessLayer\CompRootBusinessProduct.cs:line 34
at UserInterface.CRProgram.GetProduct(ICompRootBusinessProduct product, Int32 id) in C:\Users\Dinua\documents\visual studio 2017\Projects\IoCAutofac\IocAutofac\UserInterface\CRProgram.cs:line 33
at UserInterface.CRProgram.StartHere() in C:\Users\Dinua\documents\visual studio 2017\Projects\IoCAutofac\IocAutofac\UserInterface\CRProgram.cs:line 19
at UserInterface.Program.Main(String[] args) in C:\Users\Dinua\documents\visual studio 2017\Projects\IoCAutofac\IocAutofac\UserInterface\Program.cs:line 21
Inner Exception 1:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'BusinessLayer.Validators.Validator2' can be invoked with the available services and parameters:
Cannot resolve parameter 'Shared.ILogger logger' of constructor 'Void .ctor(Shared.ILogger)'.
So my first question is: is this the right way to achieve this? If it is, how can I make sure that when resolving Validator, it looks at the shared container for the injection?
Thanks Alex
User contributions licensed under CC BY-SA 3.0