AfterMap使用IMappingAction和IHttpContextAccessor抛出错误

问题描述 投票:0回答:1

我按照例子找到了here。我有一个课程如下:

```public class FileServiceRequestMappingAction:IMappingAction {private readonly IHttpContextAccessor _httpContextAccessor;

    public FileServiceRequestMappingAction(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void Process(FileServiceRequest source, FileSearchRequest destination)
    {
        try
        {
            destination.Filter = "somevalue";

            var storageType = "somestoragetype";

            if (_httpContextAccessor.HttpContext.Request.Path.Value.ToLower().Contains("/patha/"))
            {
                storageType = "someothertypeofstorage";
            }

            if (!string.IsNullOrWhiteSpace(destination.Filter))
            {
                destination.Filter += $" and storageType eq {storageType}";
            }
            else
            {
                destination.Filter += $"storageType eq {storageType}";
            }
        }
        catch (Exception ex)
        {
            throw new TranslationException($"Error in filter. {ex.Message}");
        }
    }
}

```

在我的创业公司,我有:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddAutoMapper(typeof(FileServiceRequestMappingAction).Assembly);

当我启动应用程序并执行我的api查询调用地图时,我收到以下错误: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at AutoMapper.Configuration.MappingExpression`2.<>c__60`1.<AfterMap>g__AfterFunction|60_0(TSource src, TDestination dest, ResolutionContext ctxt) in C:\projects\automapper\src\AutoMapper\Configuration\MappingExpression.cs:line 450 at lambda_method(Closure , FileServiceRequest , FileSearchRequest , ResolutionContext ) at lambda_method(Closure , Object , Object , ResolutionContext ) at AutoMapper.Mapper.AutoMapper.IMapper.Map[TDestination](Object source) in C:\projects\automapper\src\AutoMapper\Mapper.cs:line 207

错误消息是: No parameterless constructor defined for this object.

我确信我做错了什么。请注意,在startup.cs文件中,我尝试过services.AddAutoMapper(typeof(Startup).Assembly);同样。它也不起作用。

任何想法,任何人?

谢谢。

automapper
1个回答
0
投票

仅供参考:我通过切换到使用配置文件而不是直接映射来修复此问题。

© www.soinside.com 2019 - 2024. All rights reserved.