Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET cannot add AutoMapper service

I installed AutoMapper

<PackageReference Include="AutoMapper" Version="15.0.1" />

and created my profile (AutoMapperConfigurationProfile.cs):

using AutoMapper;
using Invoices.Api.Models;
using Invoices.Data.Entities;

namespace Invoices.Api
{
    public class AutoMapperConfigurationProfile: Profile
    {
        public AutoMapperConfigurationProfile() 
        {
            CreateMap<Person, PersonDto>().ReverseMap();
        }
    }
}

It is in the same folder as Program.cs.

But when I wanted to add AutoMapper in program.cs like this:

builder.Services.AddAutoMapper(typeof(AutoMapperConfigurationProfile));

I get an error:

Error CS1503 Argument 2: cannot convert from 'System.Type' to 'System.Action<AutoMapper.IMapperConfigurationExpression>' Invoices.Api

This is my whole Program.cs:

using Invoices.Data.Repositories.Interfaces;
using Invoices.Data.Repositories;
using System.Text.Json;
using Invoices.Api;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });

builder.Services.AddScoped<IPersonRepository, PersonRepository>();
builder.Services.AddAutoMapper(typeof(AutoMapperConfigurationProfile));

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

I can't figure out why. Thank you very much

like image 764
Filip Z Avatar asked Dec 22 '25 22:12

Filip Z


2 Answers

Assuming that you are using AutoMapper version 13.0 and above, which had been migrated to the core package instead of depending on AutoMapper.Extensions.Microsoft.DependencyInjection package.

The way you are registering the profile(s) is for using the old AutoMapper (before version 13.0).

You should provide the Action for registering the profile:

builder.Services.AddAutoMapper(cfg =>
{
    cfg.AddProfile(new AutoMapperConfigurationProfile());
});

Refer from the official docs, you can also achieve with:

builder.Services.AddAutoMapper(cfg => {}, 
    typeof(AutoMapperConfigurationProfile).Assembly);

// Or
builder.Services.AddAutoMapper(cfg => {}, 
    Assembly.GetExecutingAssembly());

which it will scan the assembly and register the profile (class) that implements Profile.

like image 70
Yong Shun Avatar answered Dec 24 '25 11:12

Yong Shun


According to the Automapper 15.0 Upgrade guide, Automapper now requires a license. You can get this by visiting this Lucky Penny Software link and registering using your Google, Microsoft or Github account, you will receive an email verification link.

Once you have verified your account, the rest is pretty straightforward.

  1. Get Automapper

enter image description here

2. Pick your license type based on the need (I picked community for this usecase)

enter image description here

3. Create your one year license.

enter image description here

Once you've done that, you'll proceed to copy your license and set it via the configuration:

services.AddAutoMapper(cfg => {
    cfg.LicenseKey = "<License Key Here>";
});

Since the AddAutoMapper overloads now all require the Action<IMapperConfigurationExpression> parameter this is how you set it up:

// Previous
services.AddAutoMapper(typeof(Program));

// Current
services.AddAutoMapper(cfg => cfg.LicenseKey = "<License Key Here>", typeof(Program));

BONUS:

The license key can be quite long, I would advise, setting it in your appsettings.json like so:


  "AutoMapper": {
    "LicenseKey": "blehblehbleh"
  },

Then binding (or retrieving) the configuration section from your appsettings.json:

var automapperSettings = builder.Configuration.GetSection("AutoMapper");

Reading your license from the LicenseKey setting of the configuration section:

var autoMapperLicenseKey = automapperSettings["LicenseKey"];

And finally registering AutoMapper in the DI container, configuring it with your license key this way, and scanning the assembly containing Program for profiles

builder.Services.AddAutoMapper(cfg => cfg.LicenseKey = autoMapperLicenseKey, typeof(Program));

That should clear the error and get you on your way.

like image 34
alvinchesaro Avatar answered Dec 24 '25 10:12

alvinchesaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!