28

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

2
  • 4
    typeof(Program).Assembly I referred this question Commented Feb 22, 2022 at 5:26
  • Your comment is correct, you can post it as Answer so I can accept it. Commented Feb 23, 2022 at 5:26

7 Answers 7

44

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>();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the functionality of the AutoMapper.Extensions.Microsoft.DependencyInjection package is now included directly in AutoMapper (from .NET 6.0)
31

Using this line instead: typeof(Program).Assembly

1 Comment

You can drop the .Assembly bit and it'll work just the same.
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.

1 Comment

Note that the functionality of the AutoMapper.Extensions.Microsoft.DependencyInjection package is now included directly in AutoMapper (from .NET 6.0)
3

Use this line to fix:

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

1 Comment

Isn't this essentially the same suggestion as that provided by a previous answer? You root this off a different persistent class (Program, not Startup), but the basic concept is the same.
3

AutoMapper.Extensions.Microsoft.DependencyInje The display is deprecated

1 Comment

What's the work around?
0

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

services.AddAutoMapper(Assembly.GetExecutingAssembly());

1 Comment

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.
-2

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));
}

1 Comment

Core 6.0 don't have class Startup.cs

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.