我将我的应用程序更新为网络核心3,运行应用程序时出现错误 关于Automapper(至少我是说)
我已经更新了运行dotnet -outdate的相对库,然后更新了依赖项
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: MyDemo.Models.Mapping.Resolvers.DefaultResolver Lifetime: Transient ImplementationType: MyDemo.Models.Mapping.Resolvers.DefaultResolver': Unable to resolve service for type 'System.String' while attempting to activate 'MyDemo.Models.Mapping.Resolvers.DefaultResolver'.) (Error while validating the service descriptor 'ServiceType: MyDemo.Models.Mapping.Resolvers.MoneyResolver Lifetime: Transient ImplementationType: MyDemo.Models.Mapping.Resolvers.MoneyResolver': Unable to resolve service for type 'System.String' while attempting to activate 'MyDemo.Models.Mapping.Resolvers.MoneyResolver'.) (Error while validating the service descriptor 'ServiceType: MyDemo.Models.Mapping.Resolvers.TimeSpanResolver Lifetime: Transient ImplementationType: MyDemo.Models.Mapping.Resolvers.TimeSpanResolver': Unable to resolve service for type 'System.String' while attempting to activate 'MyDemo.Models.Mapping.Resolvers.TimeSpanResolver'.)
---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: MyDemo.Models.Mapping.Resolvers.DefaultResolver Lifetime: Transient ImplementationType: MyDemo.Models.Mapping.Resolvers.DefaultResolver': Unable to resolve service for type 'System.String' while attempting to activate 'MyDemo.Models.Mapping.Resolvers.DefaultResolver'.
---> System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'MyDemo.Models.Mapping.Resolvers.DefaultResolver'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.ValidateService(ServiceDescriptor descriptor)
services.AddAutoMapper(typeof(Startup));
似乎是依赖项注入错误,但无法理解
答案 0 :(得分:0)
对于您的DefaultResolver
,它具有需要传递string memberName
的构造函数。
一般来说,自定义值解析器可以使用三种方式来使AutoMapper使用
对于Custom constructor methods,您需要尝试第三个选项,例如:
var configuration = new MapperConfiguration(cfg => cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Total,
opt => opt.MapFrom(new CustomResolver())
));