3

Autofac 从版本 4 开始使用新的 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在点击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
4

0 回答 0