Cannot resolve NameValueCollection using Autofac

I am using Autofac 2.1.12 to handle dependency injection and I am having problems with one specific problem. It seems I can not solve the NameValueCollection dependency.

Consider the following code snippet:

class Foo
{
    public Foo(NameValueCollection collection) { }
}

static class Run
{
    public static void Main()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<NameValueCollection>();
        builder.RegisterType<Foo>();

        using (var scope = builder.Build())
            scope.Resolve<Foo>();
    }
}

It will crash on an unhandled DependencyResolutionException:

Circular component dependency detected: Foo โ†’ System.Collections.Specialized.NameValueCollection โ†’ System.Collections.Specialized.NameValueCollection.

However, if I replace NameValueCollection with any other type, the code works fine.

I'm doing something waving, is there something special in the NameValueCollection type that I am missing, or is it a problem with Autofac itself?

+3
1

. . Autowiring:

Autofac , .

NameValueCollection ( , ):

builder.RegisterType<NameValueCollection>().UsingConstructor();

,

builder.Register(c => new NameValueCollection());
+3

Source: https://habr.com/ru/post/1735555/


All Articles