当我对 ASP.NET MVC 控制器使用 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?请帮帮我。
升级版:
//in Global.asax
DependencyConfigure.Initialize();
//in DependencyConfigure class
public static void Initialize()
{
var builder = new ContainerBuilder();
DependencyResolver.SetResolver(
new Dependency(RegisterServices(builder))
);
}