Asp.net core 2.0 AutoMapper IValueResolver dependency injection

天大地大妈咪最大 提交于 2019-12-11 01:39:01

问题


I have tried most of the examples in the Google Results, Stackoverflow and in AutoMapper. But was not able to get the IValueResolverdependancy injection to work.

I have below service

public class StorageService : IStorageService
{
    private readonly BlobServiceSettings _blobServiceSettings;

    public StorageService(IOptions<BlobServiceSettings> blobServiceSettings)
    {
        _blobServiceSettings = blobServiceSettings.Value;
    }

    // some methods I need
}

This is my profile

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Building, BuildingEnvelope>(MemberList.None)
        .ForMember(dest => dest.ImageUrl, opt => opt.ResolveUsing<BuildingImageUrlResolver>());
    }
}

this is my IValueResolver

public class BuildingImageUrlResolver : IValueResolver<Building,         BuildingEnvelope, string>
{
    private readonly IStorageService _storageService;
    public BuildingImageUrlResolver(IStorageService storageService)
    {
        _storageService = storageService;
    }

    public string Resolve(Building entity, BuildingEnvelope envelope, string member, ResolutionContext context)
    {               
        return _storageService.MyMethod(entity.ImageFileName);
    }
}

I get the below error in my inner exception

No parameterless constructor defined for this object.

Not sure what I am doing wrong.

Thanks in advance Neo


回答1:


Lucian's suggestion is correct -- the AutoMapper.Extensions.Microsoft.DependencyInjection package is the way to go. Even if you don't want to use it, you'll have to do something similar.

I've had this very same problem and by using the extensions, you just modify the entrypoint from which you register AutoMapper and its configuration.

What the extensions do (source) is:

  1. Initializes Automapper with the configuration provided
  2. It scans for all classes you have that you could be implementing with dependency injection and registers them as transient, looking for implementations of the following:

    • IValueResolver
    • IMemberValueResolver
    • ITypeConverter
    • IMappingAction

    The assemblies that it will scan actually depend on the parameters that you provide on the call.

  3. If any of these can be actually instantiated, then they will be registered as transient implementation.
  4. And just like that, AutoMapper will request instances of these to the service provider, which will resolve them, and to do that, it will also resolve any pending dependencies.

Note that this is actually very simple -- the most difficult part is scanning the right assemblies and registering the right classes. You can do it manually too, but these extensions already take care of it for you.

Mind you, even when reflection has been improved a lot, this process is relatively slow, so try not to abuse it too much (for instance, in tests).


Finally, if none of that works for you, remember that you need to setup AutoMapper to use the dependency injection resolver too:

automapperConfiguration.ConstructServicesUsing(serviceProvider.GetService);


来源:https://stackoverflow.com/questions/49440305/asp-net-core-2-0-automapper-ivalueresolver-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!