Skip to content

Missing 'name' attribute on components/services element causes ArgumentNullException #18

Description

@dotnetjunkie

When using Autofac.Configuration v4.0.1, I found that it produces a very confusing exception when the 'name' attribute is omitted.

In case the name attribute on the element of is missing, a very generic ArgumentNullException is thrown from deep within Autofac.Configuration when ContainerBuilder.Build is called, instead of communicating the real cause.

Steps to reproduce:

The following XML "autofac.xml" configuration file:

xml version="1.0" encoding="utf-8" ?>
<autofac>
  <components name="0">
    <type>AutofacXmlConsole.Calculator, AutofacXmlConsoletype>
    <services  type="AutofacXmlConsole.ICalculator, AutofacXmlConsole" />
  components>
autofac>

With the following console application:

namespace AutofacXmlConsole
{
    using System;
    using Autofac;
    using Autofac.Configuration;
    using Microsoft.Extensions.Configuration;

    public interface ICalculator { }
    public class Calculator : ICalculator { }

    class Program
    {
        static void Main(string[] args)
        {
            var configBuilder = new ConfigurationBuilder();
            configBuilder.AddXmlFile("autofac.xml");
            var config = configBuilder.Build();

            var module = new ConfigurationModule(config);
            var builder = new ContainerBuilder();
            builder.RegisterModule(module);

            // Throws exception
            var container = builder.Build();

            container.Resolve<ICalculator>();
        }
    }
}

Running the code results in the following exception:

System.ArgumentNullException: 'Value cannot be null.'

With the following stack trace:

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.d__1.MoveNext()
at Autofac.Configuration.Core.ComponentRegistrar.RegisterComponentServices[TReflectionActivatorData,TSingleRegistrationStyle](IConfiguration component, IRegistrationBuilder`3 registrar, Assembly defaultAssembly)
at Autofac.Configuration.Core.ComponentRegistrar.RegisterConfiguredComponents(ContainerBuilder builder, IConfiguration configuration)at Autofac.Module.Configure(IComponentRegistry componentRegistry)
at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
at AutofacXmlConsole.Program.Main(String[] args)

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions