'Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'CommentAppService'.)

Hi I am Getting this error BookEvent.Project.UserDomain.AppServices.ICommentAppService Lifetime: Transient ImplementationType: BookEvent.Project.UserDomain.AppServices.CommentAppService': Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'BookEvent.Project.UserDomain.AppServices.CommentAppService'.) when I run my application. Since I not able to figure out what I am doing work. I have no idea how to resolve it. Can anybody help me please.

CommentAppService.cs

public class CommentAppService:AppService, ICommentAppService
    {
        private IMapper _mapper;
        private ICommentRepository _commentRepository;
        public CommentAppService(ICommentUnitOfWork unitOfWork, ICommentRepository commentRepository, IMapper mapper, IExceptionManager exceptionManager) : base(unitOfWork, exceptionManager)
        {

            this._mapper = mapper;
            this._commentRepository = commentRepository;
        }
   }

CommentUnitOfWork.cs

public class CommentUnitOfWork : UnitOfWork, ICommentUnitOfWork
    {
        public CommentUnitOfWork(BookDomainDbContext dbContext, IExceptionManager exceptionManager) : base(dbContext, exceptionManager)
        {

        }
    }

Startup.cs

   services.AddTransient<IUserAppService, UserAppService>();
   services.AddTransient<IEventAppService, EventAppService>();
   services.AddTransient<ICommentAppService, CommentAppService>();

CommentController.cs

 public class CommentController : Controller
    {

        private readonly ILogger<CommentController> _logger;
        private readonly ICommentAppService _commentAppService;
        private readonly IMapper _mapper;
        public string Message { get; set; }

        public CommentController(ILogger<CommentController> logger, ICommentAppService commentAppService, IMapper mapper)
        {
            _logger = logger;
            _commentAppService = commentAppService;
            _mapper = mapper;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult CreateComment(CommentDTO item)
        {
            item.Date = DateTime.Now;
            var result = _commentAppService.Create(item);
            if (result.IsSuccess)
            {
                this._logger.LogInformation(result.MainMessage.Text);
            }
            else
            {
                Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
                this._logger.LogError(Message);
                return View("CreateEvent");
            }
            return RedirectToAction("Index", "Home");
        }

        public IActionResult CreateComment()
        {
            return View(new CommentViewModel());
        }

Thanks in Advance.



Solution 1:[1]

The exception alredy tells you exacly what is going wrong:

Unable to resolve service for type 'BookEvent.Project.UserDomain.UoW.ICommentUnitOfWork' while attempting to activate 'BookEvent.Project.UserDomain.AppServices.CommentAppService'.)

So when DI is trying to instanciate CommentAppService it is looking for ICommentUnitOfWork because it is expected in the constructor of CommentAppService but it cannot find an implementation for ICommentUnitOfWork.

In your startup you are registering three things

services.AddTransient<IUserAppService, UserAppService>();
services.AddTransient<IEventAppService, EventAppService>();
services.AddTransient<ICommentAppService, CommentAppService>();

but none of them is ICommentUnitOfWork. Just register it in the startup and it will work fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nannanas