Autofac DI .NET MVC控制器没有默认构造函数

时间:2013-07-22 08:43:00

标签: asp.net-mvc constructor dependency-injection controller autofac

当我对ASP.NET MVC Controller使用Ajax查询时出现错误:输入' Web.Implementation.Controllers.MyController'没有默认构造函数。

我的代码:

    //In My Controller

    private IDataService DataService;

    //I try to delete this constructor, 
    //because I use DI and don't want to create a CurrentContext and DataService.
    public MyController()
    {
       var CurrentContext = new CurrentContext();
       DataService = new DataService(CurrentContext);
    }

    public MyController(IDataService dataService)
    {
        DataService = dataService;
    }

    //In Autofac Init
    builder.RegisterType<DataService>().As <IDataService>();

在这种情况下如何使用Autofac DI? 请帮帮我。

UPD:

//in Global.asax
DependencyConfigure.Initialize();

//in DependencyConfigure class
 public static void Initialize()
        {
            var builder = new ContainerBuilder();
            DependencyResolver.SetResolver(
                new Dependency(RegisterServices(builder))
                );
        }

0 个答案:

没有答案