问题
I want to register assembly types which has multiple constructors Autowiring chooses the wrong constructor and want to specify it as I do in RegisterType
builder.RegisterType(typeof(IController))
.UsingConstructor(typeof(IUnitOfWork));
But when I do this
builder.RegisterAssemblyTypes(typeof(IController).Assembly)
.UsingConstructor(typeof(IUnitOfWork));
I get
"No matching constructor exists on type 'System.Object'."
I think this is due to the fact that assembly type is a bit more complex than i thought, but the problem remains open
What should I do?
回答1:
By doing
builder.RegisterType(typeof(IController))
.UsingConstructor(typeof(IUnitOfWork));
You register IController inside Autofac container and tell it that it should use a constructor with a IUnitOfWork parameter.
The UsingConstructor method doesn't work with RegisterAssemblyTypes but in your case you can use FindConstructorWith method.
builder.RegisterAssemblyTypes(typeof(IController).Assembly)
.FindConstructorsWith(t => new[] {
t.GetConstructor(new[] { typeof(IUnitOfWork) })
})
.As<IController>();
来源:https://stackoverflow.com/questions/12782680/registerassemblytypes-with-usingconstructor