Xml 構成を使用して PARAMETER を設定しようとすると、次のエラーが発生します。
タイプ 'LM.AM.Core.Services.EmailService' の 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' で見つかったコンストラクターは、利用可能なサービスとパラメーターで呼び出すことができません: パラメーター 'System.String testSmtp' を解決できませんコンストラクター '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 パラメーターを認識していることを確認し、Wiki をできる限り詳しく調べましたが、何らかの理由でパラメーターが唯一のコンストラクターで解決されず、上記のエラーが発生しています。
これは非常に簡単に実行できるはずです。これを機能させるために私ができることについて、誰かが提案を提供できますか?