Search code examples
.net-coredependenciesautomapper

How can register profiles in Automapper from different assemblies?


I have an application (NET Core) with many assemblies:

  • WebAPI (contain view models and consume DTO)
  • Services (contain DTO and consume Domain entities)

On WebAPI assembly I registered automapper profiles automatically with this line:

services.AddAutoMapper();

With this line I can convert view models to DTO (and backwards)

But I need register profiles located on Services layer to convert DTO to Domain entities (and backwards)

Evidently, Automapper not found this profiles.

What's the best way to register profiles from different assemblies?


Solution

  • I use services.AddAutoMapper(params Assembly[] assemblies).

    for example:

    services.AddAutoMapper(
        typeof(Startup).GetTypeInfo().Assembly, 
        typeof(Class_In_Other_Assembly).GetTypeInfo().Assembly
    );