Autofac如何指定构造函数MVC

时间:2015-09-14 15:05:47

标签: c# asp.net-mvc asp.net-mvc-4 dependency-injection autofac

我正在使用Autofac进行依赖注入。

我在申请开始时有以下代码。

var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType(typeof(HolidayService)).AsImplementedInterfaces();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

我有MVC控制器如下

public class MyController : Controller{
    private IHolidayService _HolidayService;    

    public MyController (IHolidayService holidayService) {
        //I want this constructor to be called**
        this._HolidayService = holidayService;
    }

    private MyController () {
        //but this constructor is called unfortunately

    } 
}

我希望调用参数化构造函数。但它调用默认构造函数。

如何调用参数化构造函数?

0 个答案:

没有答案