ASP.NET Core 2.0 AutoMapper IValueResolver依赖项注入 [英] Asp.net core 2.0 AutoMapper IValueResolver dependency injection

查看:255
本文介绍了ASP.NET Core 2.0 AutoMapper IValueResolver依赖项注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了Google结果,Stackoverflow和AutoMapper中的大多数示例.但是无法使IValueResolverdependency注入起作用.

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.

我有以下服务

public class StorageService : IStorageService
{
    private readonly BlobServiceSettings _blobServiceSettings;

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

    // some methods I need
}

这是我的个人资料

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

这是我的IValueResolver

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.

不确定我做错了什么.

先谢谢了 新

推荐答案

Lucian的建议是正确的-

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.

我遇到了同样的问题,通过使用扩展,您只需修改从中注册AutoMapper及其配置的入口点即可.

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.

扩展功能()是:

  1. 使用提供的配置初始化Automapper
  2. 它将扫描您可以通过依赖项注入实现的所有类,并将它们注册为临时类,以查找以下各项的实现:

  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
  • IValueResolver
  • IMemberValueResolver
  • ITypeConverter
  • IMappingAction

它将扫描的程序集实际上取决于您在调用中提供的参数.

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

请注意,这实际上非常简单-最困难的部分是扫描正确的程序集并注册正确的类.您也可以手动进行操作,但是这些扩展已经为您解决了问题.

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

最后,如果这些都不适合您,请记住,您还需要设置AutoMapper来使用依赖项注入解析器:

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

这篇关于ASP.NET Core 2.0 AutoMapper IValueResolver依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆