在Autofac / Nancy应用程序

时间:2016-12-20 11:09:54

标签: c# autofac nancy autofac-configuration

自版本4以来的Autofac使用新的Microsoft配置。 According to the documentation以下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>

如果我尝试使用以下代码注册此模块,我会在&#39; typeName&#39;上遇到意外的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

0 个答案:

没有答案