locked
ServiceLayer dependency problem in DotNetCore Web API controller after adding Swagger package - RRS feed

  • Question

  • User-215451226 posted

    Hi.

    A problem I am having is with .NetCore WebAPI type project. Earlier running fine, throwing exception after adding swagger packages and resp. modifying the startup.cs class. Coreswagger.API is my project name.

    Exception -->
    Unable to resolve service for type 'CoreSwagger.API.Services.CarDataService' while attempting to activate
    'CoreSwagger.API.Controllers.CarController'.

    CarDataService.cs class -->

    public class CarDataService:IDisposable
        {
            public List<CarModel> GetCarsByMaker(string brandname)
            {
                try
                {
                    CarDataMock objCM = new CarDataMock();
                    return objCM.cars.Where(c => c.Brand == brandname).ToList();
                }
                catch(Exception exp)
                {
                    throw exp;
                }
            }
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
        }

    CarController.cs class -->

    [Route("api/[controller]")]
        public class CarController : Controller
        {
            protected CarDataService cService { get; set; }
    
            public CarController(CarDataService cserv)
            {
                cService = cserv;
            }
    
            /// <summary>
            /// Get Cars by maker
            /// </summary>
            [Route("GetCarsByMaker/term")]
            [HttpGet]
            public JsonResult GetCarsByMaker(string makername)
            {
                try
                {
                    var carList = cService.GetCarsByMaker(makername);
                    return Json(carList);
                }
                catch(Exception exp)
                {
                    throw exp.InnerException;
                }
            }
    
        }

    Simple straightforward that ran Ok before I added swagger related stuff. Now that above exception shows up. It talks about problems with Dependency Injection(??).
    But if that be so, it shouldn't have worked before adding swagger.

    Where's the glitch. What extra to be added to Startup.cs class.

    Thursday, June 25, 2020 5:45 PM

Answers

  • User711641945 posted

    Hi PGChoudhury,

    Are you sure you have called the CarController api before adding swagger?Only if you call the api,it would get error. I can get the same error message if I do not DI the CarDataService before adding swagger packages.

    Register like below in Startup.cs:

    services.AddScoped<CarDataService>();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 7:25 AM

All replies

  • User711641945 posted

    Hi PGChoudhury,

    Are you sure you have called the CarController api before adding swagger?Only if you call the api,it would get error. I can get the same error message if I do not DI the CarDataService before adding swagger packages.

    Register like below in Startup.cs:

    services.AddScoped<CarDataService>();

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 7:25 AM
  • User-215451226 posted
    Yes. It ran fine. Before swagger packages were installed.
    I don't see any logical meaning why it fails afterwards.

    Will add the scoped to startup.cs. see if it works.
    Friday, June 26, 2020 11:37 AM
  • User475983607 posted

    Yes. It ran fine. Before swagger packages were installed.
    I don't see any logical meaning why it fails afterwards.

    Will add the scoped to startup.cs. see if it works.

    I think you missed Rena Ni's point.  The order of service registration matters.

    Keep in mind, it's very hard to provide assistance when the source of the error message is not shared.   The error is telling you that the DI configuration has an issue and you did not share the configuration. 

    Friday, June 26, 2020 11:47 AM
  • User-215451226 posted

    Hi. It works. added the block in startup.cs in appr. place.

    @mgebhard - what syrprised me was why would adding swagger packages[and its registration] hamper invokation of the controller that ran fine priorly. even without any registration of my cardataservice in startup[now it needs explicitly added].  hope u understand.

    Saturday, June 27, 2020 6:09 AM