我在 .NET CORE 应用程序中使用 AutoMapper。我在 Startup.cs
方法文件中的 ConfigureServices.cs
中配置了 AutoMapper,如下所示:
public void ConfigureServices(IServiceCollection services)
{
/* Some Code */
/* AutoMapper Configuration */
services.AddAutoMapper(typeof(AutoMapperProfileConfiguration).GetType().Assembly);
services.AddMvcCore();
/* Some Code */
}
和 AutoMapperProfileConfiguration
如下:
public class AutoMapperProfileConfiguration : Profile
{
public AutoMapperProfileConfiguration()
{
CreateMap<LoginRequestDto, Users>();
}
}
然后我在以下课程中使用了这个映射器:
public LoginResponseDto CreateAnAccount(LoginRequestDto loginRequestDto)
{
var userInfo = _mapper.Map<Users>(loginRequestDto);
userInfo.Id = Guid.NewGuid();
_context.InsertItem<Users>(userInfo);
var insertedUserDetails = _context.GetItemById<Users>(userInfo.Id);
insertedUserDetails.Wait();
return _mapper.Map<LoginResponseDto>(insertedUserDetails.Result);
}
但是当我尝试将 loginRequestDto
中存在的数据映射到 Users
类时,它给了我以下异常:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Object -> Users
System.Object -> NeighborhoodHelpers.UserMicroservice.Entities.Models.Users
at lambda_method(Closure , Object , Users , ResolutionContext )
at NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.DataAccessProvider\UserDataAccess\UserDataAccess.cs:line 29
at NeighborhoodHelpers.UserMicroservice.Services.UserServices.UserService.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.Services\UserServices\UserService.cs:line 22
at NeighborhoodHelpers.UserMicroservice.API.Controllers.LoginController.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.API\Controllers\LoginController.cs:line 34
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我使用的是 .NET Core 3.1。请帮我解决一下这个。我无法使用 AutoMapper 功能。
答案 0 :(得分:0)
在您的代码中,看起来 AutoMapper 的配置没有看到定义的配置文件,为了更正它,我建议使用以下两个选项之一。< /p>
选项 1: NuGet 包 AutoMapper
Startup.cs
中调用CustomAutoMapper.cs :
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp.CustomConfiguration
{
public static class CustomAutoMapper
{
public static void AddCustomConfiguredAutoMapper(this IServiceCollection services)
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
}
}
}
在 Startup.cs 中:
services.AddCustomConfiguredAutoMapper();
选项 2: 使用 AutoMapper.Extensions.Microsoft.DependencyInjection Nuget 包(我个人更喜欢使用它两年多,因为我在我的解决方案中使用了多个程序集并且此包为 Profiles 提供自动发现,因为它自动扫描从 AutoMapper 的 Profile 类继承的任何类的解决方案。
只需在您的 Startup.cs 中:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
答案 1 :(得分:0)
如下更改您的 Startup.cs:
services.AddAutoMapper(typeof(AutoMapperProfileConfiguration));
答案 2 :(得分:0)
查看 Users 和 LoginRequestDto 的属性可以更轻松地回答您的问题。因为从错误消息中可以理解,有些属性需要“忽略”,或者您正在执行诸如将字典转换为列表之类的操作。
如果您可以展示您的课程,也许我们可以相应地安排以下行并解决问题。
CreateMap<LoginRequestDto, Users>();