ArgumentNullException when using new XML configuration in Autofac / Nancy app

225 views Asked by At

Autofac since version 4 uses the new Microsoft Configuration. According to the documentation the following XML-file should be valid.

<?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>

If I try to register this module using the following code, I get an unexpected ArgumentNullException exception on 'typeName' when hitting builder.RegisterModule(module).

var configBuilder = new ConfigurationBuilder();
configBuilder.AddXmlFile("autofac.xml");
var module = new ConfigurationModule(configBuilder.Build());
container.Update(builder => builder.RegisterModule(module));

Using the following JSON file and code works fine however.

{
    "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));

Does anyone have a suggestion what I am doing wrong?

Added extra information: if I'm creating a new container instead of updating an existing one, the exception does not trigger. Also, this is using Nancy-specific update syntax, so something may be going on in there.

I'm adding the stacktrace below:

{"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

There are 0 answers