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