Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'

Tags:

c#

autofac

When I try to set a PARAMETER using the Xml Configuration I get the following error:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'LM.AM.Core.Services.EmailService' can be invoked with the available services and parameters: Cannot resolve parameter 'System.String testSmtp' of constructor 'Void .ctor(System.String)'.

Here are the relevant files:

web.config

  <configSections>     <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" />   </configSections>    <autofac>     <components>       <component type="LM.AM.Core.Services.EmailService , LM.AM.Core" service="LM.AM.Core.Infrastructure.Services.IEmailService , LM.AM.Core.Infrastructure">         <parameters>           <parameter name="testSmtp" value="abc" />         </parameters>       </component>     </components>   </autofac> 

Service Class

public class EmailService : IEmailService {     public string _testSmtp;      public EmailService (string testSmtp)     {         _testSmtp = testSmtp;     } } 

Registration

builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance(); 

Global.asax

var builder = new ContainerBuilder(); builder.RegisterModule(new ConfigurationSettingsReader("autofac"));  builder.RegisterModule<Core.ModuleInstaller>();  builder.RegisterControllers(typeof(MvcApplication).Assembly); AutofacContainer.Container = builder.Build();  var emailSvc = AutofacContainer.Container.Resolve<IEmailService>(); 

I've checked the container is aware of the xml parameter and I've followed the Wiki as close as I can, but for some reason the parameter is not resolving on the only constructor and I'm receiving the above error.

This should be pretty simple to get going. Can anyone provide some suggestions on what I can try to get this working?

like image 920
Dion Avatar asked Apr 15 '13 01:04

Dion


People also ask

What is wrong with Autofac test scenarios?

Studying the error message further reveals some strange behavior, Autofac is complaining that the type 'Autofac.Test.Scenarios.ScannedAssembly.HasDeferredEnumerable+<Get>d__0' cannot be resolved because of some 'Int32 <>1__state' parameter required in a 'Void .ctor (Int32)' constructor.

How do I resolve multiple interfaces in Autofac?

A common pattern used when working with Autofac (or any other IoC containers) is to register multiple implementations of an interface, then resolve them all and call a method on each implementation (e.g.: an IEventHandler interface that you use notify all registered handlers that something happened). Take the following example:

Is there a concrete implementation of IEnumerable<ihavedeferredenumerable> in Autofac?

Since Autofac is an open source project I checked out the repository, wrote a failing test and step-by-step debugged my way through the type registration process. This is how I found out that a concrete implementation was discovered for IEnumerable<IHaveDeferredEnumerable>, even though there is none in my scanned assembly.


1 Answers

You have regiestered your EmailService two times.

Once in the web.config and once with

builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance(); 

If you have the line above in the Core.ModuleInstaller then it will override the web.config configuration. And because here you haven't specified the parameter Autofac throws an exception.

So to solve this just remove the EmailService registration from the Core.ModuleInstaller module.

If you use the Core.ModuleInstaller multiple places and you need to have the EmailService registration there then you need to change the order of the Module loading:

var builder = new ContainerBuilder(); builder.RegisterModule<Core.ModuleInstaller>(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); 

or tell Autofac to not override the registration of EmailService if it already exists with PreserveExistingDefaults:

builder.RegisterType<EmailService>().As<IEmailService>()        .SingleInstance().PreserveExistingDefaults(); 
like image 85
nemesv Avatar answered Sep 23 '22 17:09

nemesv