我在 asp mvc 中使用光注入。
在 asp mvc 的 global.asax 中使用此代码。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var container = new ServiceContainer();
container.RegisterControllers();
container.RegisterControllers(typeof(Areas.Admin.AdminAreaRegistration).Assembly);
container.Register<INewsService, NewsService>(new PerScopeLifetime());
container.Register<ICategoryService, CategoryService>(new PerScopeLifetime());
container.EnableMvc();
}
在控制器中:
private readonly INewsService _newsservice;
private readonly ICategoryService _categoryservice;
public AdminController(INewsService newsservice, ICategoryService categoryservice)
{
_newsservice = newsservice;
_categoryservice = categoryservice;
}
但它告诉我这个错误:
未解决的依赖[目标类型:DA.Service.Service.NewsService],[参数:存储库(DA.Data.Repository.Repository
1[DA.Data.Domain.News])], [Requested dependency: ServiceType:DA.Data.Repository.Repository
1[DA.Data.Domain.News],ServiceName:]
有什么问题 ?