问题
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?
回答1:
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();
回答2:
I had created a constructor where there was none before and made it private, therefore there was default constructor so I got this error. Had to make my constructor public.
回答3:
After an undo, my VisualStudio-editor still knew the file, but my running code did not. I had to add the file to the project (again).
TLDR: Where did it go wrong?
I had added the class-file, but later undid all (instead of some) changes to the project-file (after including too many packages). This accidentally removed the file from the project(file), although still present on disk and still open in my VS-editor.
I did not get any compilation-errors, because we have a generic type-based-registration. Something like this:
var myTypes =
from assembly in myAssemblies
from type in assembly.GetTypes()
where ...;
foreach (var myImplementation in myTypes)
{
builder.RegisterGeneric(myImplementation).As(typeof(IMyInterface<>));
}
Obviously not an answer for the OP, but maybe someone with the same symptom also ends up here.
来源:https://stackoverflow.com/questions/16006248/none-of-the-constructors-found-with-autofac-core-activators-reflection-defaultc