Error with optional parameters and AnyConcreteTypeNotAlreadyRegisteredSource

101 views
Skip to first unread message

dm.ch...@gmail.com

unread,
Dec 17, 2015, 8:44:36 PM12/17/15
to Autofac

Hi there

I need to use the AnyConcreteTypeNotAlreadyRegisteredSource, to avoid having to explicitly register every concrete type we use.
Unfortunately, I'm having a real issue attempting to use it when we have classes with optional string constructor parameters.

e.g.

        public class TypeWithCtorParam : IInterface
        {
            public TypeWithCtorParam(string stringParam = "MyString") {}
        }

It throws an error

Autofac.Core.DependencyResolutionException: Cannot choose between multiple constructors with equal length 1 on type 'System.String'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered.

I've attached an nunit test class repro-ing the issue, but basically, to cut it down to the bare bones

- If the class is registered AsSelf or against its interface, it is resolved fine

builder.RegisterType<TypeWithCtorParam>();
...
scope.Resolve<TypeWithCtorParam>();  // Fine. Yay!


- If the class is registered AsSelf and/or against its interface, and AnyConcreteTypeNotAlreadyRegisteredSource is used, it throws the error. I would expect this to not be a problem - I mean, it's already explicitly registered, surely it should be excluded from the registration source!

builder.RegisterType<TypeWithCtorParam>();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
...
scope.Resolve<TypeWithCtorParam>();  // Error! Boo!


- If the class is not explicity registered, and AnyConcreteTypeNotAlreadyRegisteredSource is used, it throws the error.

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
...
scope.Resolve<TypeWithCtorParam>();  // Error! Boo!

I can exclude the types that aren't working, but it'd be nicer not to have to. Bug? Or am I missing something?

Thanks much
AutofacTests.cs

dm.ch...@gmail.com

unread,
Dec 17, 2015, 9:03:55 PM12/17/15
to Autofac
Actually, I can't even seem to exclude the bad types :(

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(t => t != typeof (TypeWithCtorParam)));

Still fails with the same error on resolution. What can I do? Aside from replacing all our optional params with required, or not using AnyConcreteTypeNotAlreadyRegisteredSource

Alex Meyer-Gleaves

unread,
Mar 15, 2016, 9:12:57 AM3/15/16
to Autofac
Sorry for the late reply. I think it's the optional string parameter that will be causing the problem. Filtering that out should do the trick.

builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource(t => !t.IsAssignableTo<string>()));

I'm going to raise an issue as this should be ignored similar to what would be the case with an int parameter. The optional string is obviously matching some criteria that it shouldn't when AnyConcreteTypeNotAlreadyRegisteredSource is added.

Alex Meyer-Gleaves

unread,
Mar 15, 2016, 9:21:36 AM3/15/16
to Autofac
Thanks for providing the unit tests. I've added an issue for this one.


Hopefully the string type predicate filter sorts it out in the meantime.
Reply all
Reply to author
Forward
0 new messages