带有IMappingAction和IHttpContextAccessor的AfterMap抛出错误

时间:2018-09-18 13:33:15

标签: automapper

我遵循了发现here的示例。我的课如下:

``` 公共类FileServiceRequestMappingAction:IMappingAction     {         私有只读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);。也一样也不行。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

仅供参考:我通过改用个人资料而非直接地图来解决此问题。