Open Closed

How to use dependency injection in AutoMapper? #2354


User avatar
0
nhontran created

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v3.3.2
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

Hi, I am trying to figure out how to inject a service in AutoMapper profile, I have tried the IMappingAction method from the link below: https://docs.automapper.org/en/stable/Before-and-after-map-actions.html

but got this exception:

[21:31:09 ERR] Error mapping types.
Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1[[TestA, TestProject.Application.Contracts, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null]]
AutoMapper.AutoMapperMappingException: Error mapping types.
Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1[[TestA, TestProject.Application.Contracts, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null]]
 ---> System.MissingMethodException: No parameterless constructor defined for type 'TestProject.TestMappingAction'.
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at AutoMapper.Configuration.MappingExpressionBase`3.<AfterMap>g__AfterFunction|11_0[TMappingAction](TSource src, TDestination dest, ResolutionContext ctxt)
   at lambda_method(Closure , Object , List`1 , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method(Closure , Object , List`1 , ResolutionContext )
   at Volo.Abp.AutoMapper.AutoMapperAutoObjectMappingProvider.Map[TSource,TDestination](Object source)
   at Volo.Abp.ObjectMapping.DefaultObjectMapper.AutoMap[TSource,TDestination](Object source)
   at Volo.Abp.ObjectMapping.DefaultObjectMapper.Map[TSource,TDestination](TSource source)

What did I miss in my configuration? Any help would be greatly appreciated.


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    It requires the abp version >= 4.2

    https://github.com/abpframework/abp/pull/6868

  • User Avatar
    0
    nhontran created

    Hi @maliming, thank you for your prompt reply.

    We dont have plan to upgrade the version yet, is there any workaround or any other solution to inject a service in AutoMapper?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi nhontran

    You can try to copy the code from the PR.

  • User Avatar
    0
    nhontran created

    Hi @maliming, the PR updated the AbpAutoMapperModule.cs, may I know how can I override this module file?

    so far, I know how to override the application service/controller only: https://docs.abp.io/en/abp/4.4/Customizing-Application-Modules-Overriding-Services

    Thank you.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Copy the commit code to your project.

    https://github.com/abpframework/abp/commit/03540ca3b0eb5f3ab4f136bcac1be83f173226e2

  • User Avatar
    0
    nhontran created

    Hi @maliming, I have followed the code but it does not work, I have sent the code via email, could you please help us take a look?

    Thank you.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I shared a new project with you. Please check.

  • User Avatar
    0
    nhontran created

    Hi @maliming,

    It works perfectly. Thanks a lot for your support

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on June 20, 2025, 11:20