バージョン 4 以降の Autofac は、新しい Microsoft 構成を使用します。ドキュメントによると、次の XML ファイルは有効である必要があります。
<?xml version="1.0" encoding="utf-8" ?>
<autofac defaultAssembly="Reinaerdt.Converter.WebApp">
<components name="0">
<type>Reinaerdt.Converter.WebApp.Authentication.ActiveDirectoryUserValidator, Reinaerdt.Converter.WebApp</type>
<services name="0"
type="Reinaerdt.Converter.WebApp.Authentication.IActiveDirectoryUserValidator, Reinaerdt.Converter.WebApp" />
</components>
</autofac>
次のコードを使用してこのモジュールを登録しようとすると、ArgumentNullException
「typeName」をヒットすると予期しない例外が発生しbuilder.RegisterModule(module)
ます。
var configBuilder = new ConfigurationBuilder();
configBuilder.AddXmlFile("autofac.xml");
var module = new ConfigurationModule(configBuilder.Build());
container.Update(builder => builder.RegisterModule(module));
ただし、次の JSON ファイルとコードを使用すると問題なく動作します。
{
"defaultAssembly": "Reinaerdt.Converter.WebApp",
"components": [
{
"type": "Reinaerdt.Converter.WebApp.Authentication.ActiveDirectoryUserValidator",
"services": [
{
"type": "Reinaerdt.Converter.WebApp.Authentication.IActiveDirectoryUserValidator"
}]
}]
}
var configBuilder = new ConfigurationBuilder();
configBuilder.AddJsonFile("autofac.json");
var module = new ConfigurationModule(configBuilder.Build());
container.Update(builder => builder.RegisterModule(module));
誰かが私が間違っていることを提案していますか?
追加情報を追加: 既存のコンテナーを更新するのではなく、新しいコンテナーを作成している場合、例外はトリガーされません。また、これは Nancy 固有の更新構文を使用しているため、そこで何かが起こっている可能性があります。
以下のスタックトレースを追加しています。
{"Value cannot be null.\r\nParameter name: typeName"}
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName)
at Autofac.Configuration.Core.ConfigurationExtensions.GetType(IConfiguration configuration, String key, Assembly defaultAssembly)
at Autofac.Configuration.Core.ComponentRegistrar.RegisterConfiguredComponents(ContainerBuilder builder, IConfiguration configuration)
at Autofac.Configuration.Core.ConfigurationRegistrar.RegisterConfiguration(ContainerBuilder builder, IConfiguration configuration)
at Autofac.Configuration.ConfigurationModule.Load(ContainerBuilder builder)
at Autofac.Module.Configure(IComponentRegistry componentRegistry)
at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
at Autofac.ContainerBuilder.UpdateRegistry(IComponentRegistry componentRegistry)
at Nancy.Bootstrappers.Autofac.ComponentContextExtensions.Update[T](T context, Action`1 builderAction)
at Reinaerdt.Converter.WebApp.CustomBootstrapper.ConfigureApplicationContainer(ILifetimeScope container) in C:\Projects\Reinaerdt Converter\03 - Source\Reinaerdt.Converter.WebApp\CustomBootstrapper.cs:line 58
at Nancy.Bootstrapper.NancyBootstrapperBase`1.Initialise()
at Nancy.Owin.NancyMiddleware.UseNancy(NancyOptions options)
at Owin.AppBuilderExtensions.UseNancy(IAppBuilder builder, NancyOptions options)
at Reinaerdt.Converter.WebApp.Startup.Configuration(IAppBuilder app) in C:\Projects\Reinaerdt Converter\03 - Source\Reinaerdt.Converter.WebApp\Startup.cs:line 9