E
E
embiid2021-07-07 09:54:24
ASP.NET
embiid, 2021-07-07 09:54:24

How to inject an ASP.NET Web Api service?

How can I inject an interface for a service?

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: X.Services.Interfaces.ServiceInterfaces.IWishListItemService Lifetime: Scoped ImplementationType: X.Infrastructure.Business.Services.WishListItemService': Unable to resolve service for type 'X.Domain.Interfaces.EFInterfaces.IWishListItemRepository' while attempting to activate 'X.Infrastructure.Business.Services.WishListItemService'.) (Error while validating the service descriptor 'ServiceType: X.Services.Interfaces.ServiceInterfaces.IWishListItemService Lifetime: Scoped ImplementationType: X.Infrastructure.Business.Services.WishListItemService': Unable to resolve service for type 'X.Domain.Interfaces.EFInterfaces.IWishListItemRepository' while attempting to activate 'X.Infrastructure.Business.Services.WishListItemService'.)


Service:
public class WishListItemService : IWishListItemService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IWishListItemRepository _wishListItemRepository;
        private readonly IProductDetailsRepository _productDetailsRepository;

        public WishListItemService(IMapper mapper, IUnitOfWork unitOfWork,
            IWishListItemRepository wishListItemService, IProductDetailsRepository productDetailsRepository)
        {
            _wishListItemRepository = wishListItemService;
            _productDetailsRepository = productDetailsRepository;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
        }


Controller:
public class WishListItemController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IWishListItemService _wishListItemService;

        public WishListItemController(IWishListItemService wishListItemService, IMapper mapper)
        {
            _mapper = mapper;
            _wishListItemService = wishListItemService;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-07-07
@vabka

Register like WishListItemService.
Most likely somewhere in Startup.ConfigureServices

services.AddScoped<IWishListItemRepository, WishListItemRepository>()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question