【发布时间】:2023-03-12 18:10:02
【问题描述】:
我已经尝试了 Google 结果、Stackoverflow 和 AutoMapper 中的大多数示例。但无法让 IValueResolverdependancy 注入工作。
我有以下服务
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
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);
}
}
我的内部异常出现以下错误
No parameterless constructor defined for this object.
不知道我做错了什么。
提前致谢 尼奥
【问题讨论】:
-
错误是否标识了哪个类?无论如何,我怀疑您需要 public StorageService() 或 public BuildingImageUrlResolver()
-
@FRowe 只要我在构造函数中有一个 DI,我就会收到此错误
标签: dependency-injection automapper asp.net-core-2.0 resolver