0

This is the error I get:

System.InvalidOperationException: Unable to resolve service for type 'myBackEnd.Entities.Striper' while attempting to activate 'myBackEnd.Controllers.StripeController'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

I am working on using Automapper for the first time. I am also new to NET. I am using an HTTPPOST to get data from the front end in the format

    amount :  string;
    currency :  string;
    description :  string;
    token :  string;
    name: string;
    address_city: string;
    address_line1: string;
    address_line2: string;
    address_state: string;
    address_zip: string;
    address_country: string;

I have stripe.cs and stripeDto.cs files:

public class Striper
{
    public object Amount { get; set; }
    public string Currency { get; set; }
    public object Description { get; set; }
    public string Token { get; set; }
    public string Name { get; set; }
    public string Address_line1 { get; set; }
    public string Address_line2 { get; set; }
    public string Address_state { get; set; }
    public string Address_zip { get; set; }
    public string Address_country { get; set; }
}

stripeDto:

public class StripeDto
{
    public object Amount { get; set; }
    public string Currency { get; set; }
    public object Description { get; set; }
    public string Token { get; set; }
    public string Name { get; set; }
    public string Address_line1 { get; set; }
    public string Address_line2 { get; set; }
    public string Address_state { get; set; }
    public string Address_zip { get; set; }
    public string Address_country { get; set; }
}

This is the mapping profile file:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Striper, StripeDto>();
        CreateMap<StripeDto, Striper>();
    }
}

Finally this is the Controller:

private readonly AppDbContext _context;
private IMapper _mapper;

public StripeController(AppDbContext context, IMapper mapper)
{
     _context = context;
     _mapper = mapper;
}

public async Task<IActionResult> PostCreditCardData([FromBody] StripeDto stripeDto)
{
        Console.WriteLine("got this from the front end", stripeDto);
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

          _context.StripeDto.Add(stripeDto);
         // Instantiate source object stripe
         await _context.SaveChangesAsync();

        _striper = _mapper.Map<Striper>(stripeDto);
        return Ok(_striper);


 }

I get this error in visual studio "Unable to resolve service for type 'myBackEnd.Entities.Striper'"

Here is the startup.cs code:

services.AddAutoMapper();
5
  • 1
    Where is your AutoMapper configuration code in Startup class? Commented Feb 10, 2019 at 15:14
  • github.com/AutoMapper/… Commented Feb 10, 2019 at 15:18
  • 1
    Your error does not seem to be about AutoMaper. Do you have any properties on the StripeContoller ? Did you edit ('simplify') the posted code? Commented Feb 10, 2019 at 16:45
  • 1
    @LucianBargaoanu Whats your problem? Why have you down voted the correct answer? You did the same in past! Commented Feb 12, 2019 at 6:19
  • I had the same problem. And i am using a different project for converters/mapping. Use this code services.AddAutoMapper(typeof(MappingProfile).GetTypeInfo().Assembly); MappingProfile - can be in another project. Also you can use just CreateMap<Striper, StripeDto>().ReverseMap(); Commented Feb 13, 2020 at 13:05

3 Answers 3

2

Your AutoMapper configuration in Startup class should be as follows:

public void ConfigureServices(IServiceCollection services)
{

    // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    });

    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);

    //........
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am too afraid to use teamviewer. How can I console log stripeDto from the front end in the HTTPPOST?
I am installng teamviewer
auto mapper has an extension for this in ASP.Net Core - services.AddAutoMapper();
2

First, you must install Automapper dependency injection package:

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

Call services.AddAutoMapper() in ConfigureServices method in the Startup class.

More on this at: https://dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/

3 Comments

The author already did that. Otherwise the IMapper variable _mapper could not be used nor the controller could be instantiated.
I did add the above packages. I am getting the same error. How can I console log what I get from the front end to eliminate AutoMapper?
@RobinGüldenpfennig, You are correct, off course! :)
1

I know it's too late

but it's because of your startup and AutoMapper configuration

services.AddAutoMapper(typeof(MappingProfile).Assembly);

best regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.