Unable to resolve class library references in dotnet core 5 app

Muhammad Nabeel Iqbal 0 Reputation points
2022-04-05T07:13:24.23+00:00

I have class library and i have used that class library as a project reference in my dotnet core 5 webapi project.

And i have used it in the controller like this
190063-capture.png

But when i hit this controller from post man i get error saying:
189959-image.png

Im not sure what i am doing wrong.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,385 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 27,656 Reputation points
    2022-04-05T09:55:44.2+00:00

    The original error message means you did not register service with the IoC container in the startup.cs file.

    services.AddScoped<IAccountService, AccountService>();

    Please read the official documentation.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

    Also, please post code on the forum using the "Code Sample" button rather than a screenshot

    1 person found this answer helpful.

  2. JasonPan - MSFT 4,811 Reputation points Microsoft Vendor
    2022-04-05T08:38:11.377+00:00

    Hi @Muhammad Nabeel Iqbal

    Your VirtualAssistantsController should be changed like below. Then the VirtualAssistantsController will be successfully initialized and the services can be injected into the Controller.

    public class VirtualAssistantsController : Controller  
    {  
        private readonly ILogger<VirtualAssistantsController> _logger;  
        private readonly WebUserService _webUserService;  
        private readonly AllControllerServices _services;  
        private readonly  AccountService _accountService;  
        public HomeController(ILogger<VirtualAssistantsController> logger, WebUserService webUserService, AllControllerServices services, AccountService accountService)  
        {  
            _logger = logger;  
            _webUserService = webUserService;  
            _services = services;  
            _accountService = accountService;  
        }  
        ....  
    }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Jason

    0 comments No comments

  3. Muhammad Nabeel Iqbal 0 Reputation points
    2022-04-05T08:45:49.033+00:00

    Hello Jason thanks for writing back.
    I have a controller base class on which WebUserService webUserService, AllControllerServices services are registered.
    190113-image.png
    this is what library class looks like
    190067-image.png
    and this is how basecontoller looks like
    190085-image.png

    0 comments No comments