RegisterAssemblyTypes with UsingConstructor

柔情痞子 提交于 2020-01-01 15:36:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!