C
C#2y ago
M B V R K

✅ How to register all Automapper profiles from one project in the Startup class of a second project

Hi friends, I have a solution with the structure in the screen-shot attachement,
I use Automapper in CQRSCore and WEB projects, the CQRSCore is a class library, and the WEB project is an ASP.Net Core 7 project. Both in the CQRSCore and WEB projejcts I use Automapper profiles, and I'm looking for a simple way that using only the assembly to register all the profiles without register each one alone. So I tried :
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly(), typeof(Startup).Assembly);
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly(), typeof(Startup).Assembly);
But stiill getting the following error:
Mapping types: Branch -> BranchDto Core.Entities.Branch -> CQRSCore.DTOs.Branch.BranchDto
I hope soneone please to provide a help ifi he had this experience before. Massive thanks in advance.
5 Replies
Saber
Saber2y ago
well Assembly.GetExecutingAssembly(), typeof(Startup).Assembly probably refer to the same assembly, so you should probably pass in a reference to the other assembly that contains the mappers
M B V R K
M B V R K2y ago
Thanks for your time to help, I appreciate, Please do you know what method should I use to reference it especially it is a Class library and doesn't contain any Startup ?
Saber
Saber2y ago
I've used Assembly.Load("AssemblyName") you can use typeof(SomeTypeInOtherAssembly).Assembly as well, don't really know what is considered best practice for that
Mayor McCheese
Typeof(startup).assembly is best practice afaik; recommended by Microsoft
M B V R K
M B V R K2y ago
I fixed it by using
builder.Services.AddAutoMapper(typeof(Startup).Assembly, typeof(CQRSCore.DTOs.Branch.BranchDto).Assembly);
builder.Services.AddAutoMapper(typeof(Startup).Assembly, typeof(CQRSCore.DTOs.Branch.BranchDto).Assembly);
II appreciate your help @Deluxe , massive thanks
Want results from more Discord servers?
Add your server
More Posts