[Solved] InvalidOperationException: Unable to resolve service for type 'AutoMapper.IMapper' while attempting to activate 'Controller'. in asp.net core web Api


I was getting the below error when I was trying to access controller/get API in asp.net core API 3.1

InvalidOperationException: Unable to resolve service for type ‘AutoMapper.IMapper’ while attempting to activate ‘API_IN_COMPARE.Controllers.CategoryController’.


The below error is saying,

 Application is trying to create an instance of 'AbcController‘ but it doesn’t know how to create an instance of categoryRepository  to pass into the constructor.

InvalidOperationException: Unable to resolve service for type ‘WebApplication1.Data.CategoryRepository’ while attempting to activate ‘WebApplication1.Controllers.AbcController’.

Now I looked into startup.cs file: I registerd the service in ConfigureService method (Line No 33). whenever a Icategory is required, create a CatergoryRepository and pass the instance to In.

services.AddScoped<ICategory, CategoryRepository>();

And also, As we know that I’m using the Auto Mapper library to Category Controller class to Map CategoryEntites to Category dto class, i.e AutoMapper is an object-object mapper. The object-object mapping works by transforming an input object of one type into an output object of a different type.

We need to register auto mapper in services container in startup class, we can register this service using the AddAutomapper method which allows us to input a set of assemblies

It automatically scans for Mappingprofile contain mapping configuration,  it’s a nice way to add a mapping configuration. Create profile folder and CreateMap in the CreateProfile class constructor

public class CategoryProfile : Profile
        public CategoryProfile()

Register the interface service for dependency injection (line No 33) using addScopped() service method. In my code I’m registering my Icategory interface.

After I introduced the Mapping Framework to my controller class, to configure the auto-mapper for asp.net core MVC or APIs by using AddAutoMapper() method in ConfigureService() method of the startup.cs class and the error got resolved.

If you are getting the below error follow the link to solve the issue.

IServiceCollection’ does not contain a definition for ‘AddAutoMapper’ and no accessible extension method ‘AddAutoMapper’ accepting a first argument of type ‘IServiceCollection’ could be found (are you missing a using directive or an assembly reference?)


In this article, we understand that how to configure the auto mapper APIs in configure service method to apply mapping entities dependency injection to our controller class, I hope you find them helpful.

Shivaraju M

Over 3+ years experience in IT industry, good knowledge in . Net-based Web applications, windows applications, web services, and SPAs. I have played multiple roles related to technical delivery in multiple domains while working on over 10+ technologies. I am currently focused on continuous improvement and agile project management.

Post a Comment

Previous Post Next Post