我已经将 lightinject 的配置写到了一个单独的类中。
在 Global.ascx Application Start 我有
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);