我已经将lightinject的配置写入了一个单独的类。
在Global.ascx应用程序启动中我有
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
var container = new ServiceContainer();
DependencyResolver.SetResolver(new LightInjectDependencyResolver(container));
我的LightInjectDependencyResolver类看起来像:
public class LightInjectDependencyResolver : IDependencyResolver
{
private readonly ServiceContainer _serviceContainer;
public LightInjectDependencyResolver(ServiceContainer serviceContainer)
{
_serviceContainer = serviceContainer;
_serviceContainer.RegisterControllers();
_serviceContainer.RegisterAssembly("mydll*.dll");
_serviceContainer.EnableMvc();
}
public object GetService(Type serviceType)
{
return _serviceContainer.GetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _serviceContainer.GetAllInstances(serviceType);
}
}
我得到了死亡的黄色屏幕,错误是:
无法解析类型:System.Web.Mvc.IControllerFactory,服务名称:
编辑
我想我已经对它进行了排序,基本上我不需要DependecnyReolver,看起来它是内部的。所以你仍然可以做类似的事情:
IProductService a = DependencyResolver.Current.GetService<IProductService>();
a.GetById(1);
答案 0 :(得分:0)