24

How to configure AutoMapper in ASP.Net Core 6

I have a project which is written in .Net 3.1 so we had Startup.cs class.

I am migrating it to .net core 6

now when I put the following configuration in my .Net 6 Program.cs

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

I get error the type or namespace Startup could not be found

Any suggestions ?, how can I fix it or configure it in .net 6

MJ X
  • 8,506
  • 12
  • 74
  • 99
  • 3
    `typeof(Program).Assembly` I referred [this question](https://www.reddit.com/r/dotnet/comments/pz6xmx/setting_up_automapper_in_minimal_api/) – Tiny Wang Feb 22 '22 at 05:26
  • Your comment is correct, you can post it as Answer so I can accept it. – MJ X Feb 23 '22 at 05:26

6 Answers6

39

install package

  • dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection

in Program.cs

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

create MappingProfiles.cs

namespace XXX.XXX.Helpers;

public class MappingProfiles: Profile {
    public MappingProfiles() {
        CreateMap<Address, AddressDto>();
    }
}
sun sreng
  • 587
  • 6
  • 7
23

Using this line instead: typeof(Program).Assembly

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
  • 9
    You can drop the `.Assembly` bit and it'll work just [the same](https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/blob/986267d95da70fef4c4811b59065533137bdd304/src/AutoMapper.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs#L46-L47). – Daniel Liuzzi Mar 20 '22 at 13:38
18

For those who didn't know like me.

  1. Install AutoMapper extension for DI via NuGet or by dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
  2. Then in the Program.cs file register the service with: builder.Services.AddAutoMapper(typeof(<name-of-profile>)); (In .NET 6 we no longer have the StartUp.cs)

I used Profiles to do my mapping configuration. Here's a link from Automapper about profiles.

Khutso
  • 181
  • 1
  • 4
1

Use this line to fix:

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

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
  • Isn't this essentially the same suggestion as that provided by [a previous answer](https://stackoverflow.com/a/71216238/3025856)? You root this off a different persistent class (`Program`, not `Startup`), but the basic concept is the same. – Jeremy Caney Apr 09 '22 at 00:06
0

Install AutoMapper Package(version as per your proj requirement)

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 9.0.0

after that register a service in CinfigureServices on Startup.cs

using AutoMapper;
public void ConfigureServices(IServiceCollection services){
    services.AddAutoMapper(typeof(Startup));
}
Vikas
  • 71
  • 10
0

Try to add this line of code in your Program.cs.

services.AddAutoMapper(Assembly.GetExecutingAssembly());

Arsman Ahmad
  • 2,000
  • 1
  • 26
  • 34
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '22 at 00:48