我已经把lightinject的配置写在了一个单独的类中。
在全球。我有
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,服务名称:
编辑我想我已经对它进行了排序,基本上我不需要dependdecnyreolver,看起来它在内部做这个。所以你仍然可以这样做:
IProductService a = DependencyResolver.Current.GetService<IProductService>();
a.GetById(1);
你弄清楚了:)EnableMvc方法负责这一点。
查看http://www.lightinject.net/获取更多信息。
敬上
Bernhard Richter