简体   繁体   中英

Unresolved dependency [Class Service Layer]

i using light inject in asp mvc .

using this code in global.asax in asp mvc .

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();
    }

and in controller :

private readonly INewsService _newsservice;
    private readonly ICategoryService _categoryservice;
    public AdminController(INewsService newsservice, ICategoryService categoryservice)
    {
        _newsservice = newsservice;
        _categoryservice = categoryservice;
    }

but it show me this error :

Unresolved dependency [Target Type: DA.Service.Service.NewsService], [Parameter: Repository(DA.Data.Repository.Repository 1[DA.Data.Domain.News])], [Requested dependency: ServiceType:DA.Data.Repository.Repository 1[DA.Data.Domain.News], ServiceName:]

whats the problem ?

Apparently you need to tell Unity what is the implementation of DA.Data.Domain.News .

NewsService depends on News and it cannot be implicitly resolved (either not found, or itself has some dependencies).

Also, you are using Unity in MVC wrongly. Use the Unity for MVC NuGet.

The error states that your INewsService and ICategoryService both have dependencies (repositories) that are not registered with the container. You need to register the entire object graph. I would expect your registration to look something like:

    var container = new ServiceContainer();
    container.RegisterControllers();
    container.RegisterControllers(typeof(Areas.Admin.AdminAreaRegistration).Assembly);
    container.Register<INewsService, NewsService>(new PerScopeLifetime());
    container.Register<IRepository<News>, Repository<News>>(new PerRequestLifetime());
    container.Register<ICategoryService, CategoryService>(new PerScopeLifetime());
    container.Register<IRepository<Category>, Repository<Category>>(new PerRequestLifetime());
    container.EnableMvc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM