3

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
Travis Illig
  • 23,195
  • 2
  • 62
  • 85
Sardaukar
  • 29,034
  • 5
  • 26
  • 32
  • I don't know this tech all that well, I just happened to be reading through questions, but I did notice a difference in the XML and JSON, you have ", Reinaerdt.Converter.WebApp" in both of your elements (so the first type, followed by a comma, followed by the WebApp thing). You don't have this in the JSON. I might be suspicious of that XML, are you sure that's right? – Tim Consolazio Dec 20 '16 at 12:00
  • @TimConsolazio That XML configuration is okay. It refers to the default assembly which is also specified in the configuration file. If you use the default assembly attribute you can leave it empty in the elements. If not, you have to specify it. I have tried it without the extra information but that does not help. – Sardaukar Dec 20 '16 at 12:22
  • Do you use the latest version of autofac? – Jehof Dec 20 '16 at 12:34
  • @Jehof Yep, the latest as available on NuGet. At this time of writing version 4.2.1 – Sardaukar Dec 20 '16 at 13:11
  • Have you tried this when NOT updating an existing container and instead adding it to a new container? Also, can you post the full exception stack trace? – Travis Illig Dec 20 '16 at 16:10
  • @TravisIllig Exception does not trigger when adding it to a new container. Added the stacktrace to the post. – Sardaukar Dec 21 '16 at 08:53
  • Is the XML you added there exactly the same as what you're using, whitespace, newlines, and all? If so, have you tried without line wrapping in the type name? – Travis Illig Dec 21 '16 at 11:05
  • @TravisIllig The line wrapping is not in the original XML file. Type element is on a single line. The wrapping here is probably caused by the Stackoverflow editor. – Sardaukar Dec 21 '16 at 11:21
  • Using your exact XML I created a small assembly `Reinaerdt.Converter.WebApp` with the appropriate classes in it. I also created a simple console app that creates a container then updates it using that configuration. It worked with no exception thrown. This indicates it may not be an Autofac thing but something else. The container update syntax you're using isn't standard Autofac. Nancy perhaps? You may need to get some Nancy folks looking at this. I'll add that tag to the question. – Travis Illig Dec 21 '16 at 16:30
  • Even when using [the logic from the Nancy update extension](https://github.com/NancyFx/Nancy.Bootstrappers.Autofac/blob/master/src/Nancy.Bootstrappers.Autofac/ComponentContextExtensions.cs) I still can't repro the exception. – Travis Illig Dec 21 '16 at 16:34
  • I'll see if I can make a small repro using that specific Nancy extension. – Sardaukar Dec 21 '16 at 18:27
  • Not solved yet? We just updated our Autofac from 3.x, and I'm getting this same problem. I have a couple of components and a module in my XML file. When I qualified the components with the assembly, I got your same exception. When I set the defaultAssembly and removed the assembly name from my component registrations, they apparently succeeded, because on the next run I instead got the exception when registering the module. Unfortunately, the module is in a different assembly, so the default won't work. – Marc Chu Feb 03 '17 at 20:45

0 Answers0