C# 没有找到带有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16006248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'
提问by Dion
When I try to set a PARAMETER using the Xml Configuration I get the following error:
当我尝试使用 Xml 配置设置参数时,出现以下错误:
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)'.
在类型 'LM.AM.Core.Services.EmailService' 上找到的带有 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' 的构造函数都不能用可用的服务和参数调用:无法解析参数 'System.String testSmtp'构造函数'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
全球.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.
我已经检查了容器是否知道 xml 参数,并且我已经尽可能接近 Wiki,但是由于某种原因,该参数没有在唯一的构造函数上解析,并且我收到了上述错误。
This should be pretty simple to get going. Can anyone provide some suggestions on what I can try to get this working?
这应该很简单。任何人都可以就我可以尝试使其工作提供一些建议吗?
采纳答案by nemesv
You have regiestered your EmailServicetwo times.
你已经注册EmailService了两次。
Once in the web.config and once with
一次在 web.config 和一次
builder.RegisterType<EmailService>().As<IEmailService>().SingleInstance();
If you have the line above in the Core.ModuleInstallerthen it will override the web.config configuration.And because here you haven't specified the parameter Autofac throws an exception.
如果您在上面有一行,Core.ModuleInstaller那么它将覆盖 web.config 配置。并且因为在这里您没有指定参数 Autofac 会引发异常。
So to solve this just remove the EmailServiceregistrationfrom the Core.ModuleInstallermodule.
所以要解决这个问题,只需EmailService从Core.ModuleInstaller模块中删除注册即可。
If you use the Core.ModuleInstallermultiple places and you need to have the EmailServiceregistration there then you need to change the order of the Module loading:
如果您使用Core.ModuleInstaller多个地方并且需要在EmailService那里注册,那么您需要更改模块加载的顺序:
var builder = new ContainerBuilder();
builder.RegisterModule<Core.ModuleInstaller>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
or tell Autofacto not override the registration of EmailServiceif it already exists with PreserveExistingDefaults:
或者告诉Autofac不要覆盖注册EmailService是否已经存在PreserveExistingDefaults:
builder.RegisterType<EmailService>().As<IEmailService>()
.SingleInstance().PreserveExistingDefaults();
回答by fruitbatinshades
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.
我创建了一个以前没有的构造函数并将其设为私有,因此有默认构造函数,所以我收到了这个错误。必须公开我的构造函数。
回答by Yahoo Serious
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).
撤消后,我的 VisualStudio 编辑器仍然知道该文件,但我的运行代码不知道。我不得不将文件添加到项目中(再次)。
TLDR: Where did it go wrong?
TLDR:哪里出错了?
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 andstill open in my VS-editor.
我已经添加了类文件,但后来取消了对项目文件的所有(而不是一些)更改(在包含太多包之后)。这意外地从项目(文件)中删除了文件,尽管它仍然存在于磁盘上并且仍然在我的 VS 编辑器中打开。
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.
显然不是 OP 的答案,但也许有相同症状的人也会在这里结束。

