假如在MVC中存在基础Controller,如下:
public class BaseApiController : ApiController { public BaseApiController(IMemberService memberService) { } } public class MyController : BaseApiController { public MyController (ILogger logger,IMemberService memberService) : base(memberService) { } }
这种继承,使用Autofac的自动注入总是提示没有一个无参的构造函数的错误。请问这种我该怎么实现构造方法注入呢?
是否是有别的构造函数?
你这不是实现了么?你是想怎么样?
执行的时候如下提示:
An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.
@埋头前进的码农:
public abstract class Father { protected string name; public Father() { name = "Father"; } public Father(string fullName) { this.name = fullName; } } public class Son : Father { public Son(string fullName, int age) : base(fullName) { } public string Say() { return this.name; } }
我这样是可以的,我怀疑是你继承的Controller,有地方用他的无参数构造函数。。。
刚刚试了下,没问题啊!代码能不能贴全