當我嘗試使用XML配置我收到以下錯誤設置的參數:無構造函數的發現「Autofac.Core.Activators.Reflection.DefaultConstructorFinder」
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)'.
下面是相關的文件:
的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>
服務類
public class EmailService : IEmailService
{
public string _testSmtp;
public EmailService (string testSmtp)
{
_testSmtp = testSmtp;
}
}
註冊
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>();
我檢查容器知道XML參數的,我已經跟着維基接近盡我所能,但由於某些原因,該參數不解決唯一的構造函數,我收到上面的呃ROR。
這應該是非常簡單的。任何人都可以提供一些建議,我 可以嘗試讓這個工作?
哦,上帝,你救了我的命! – Cleiton