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

Dion picture Dion · Apr 15, 2013 · Viewed 68.8k times · Source

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?

Answer

nemesv picture nemesv · Apr 15, 2013

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();