Kianoush
Kianoush

Reputation: 47

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.Repository1[DA.Data.Domain.News])], [Requested dependency: ServiceType:DA.Data.Repository.Repository1[DA.Data.Domain.News], ServiceName:]

whats the problem ?

Upvotes: -1

Views: 2408

Answers (2)

NightOwl888
NightOwl888

Reputation: 56869

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

Upvotes: 0

hyankov
hyankov

Reputation: 4130

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.

Upvotes: 0

Related Questions