Код: Выделить всё
private readonly ApplicationDbContext data;
private readonly IMapper mapper;
public WorkoutService(ApplicationDbContext data)
{
this.data = data;
this.mapper = AutoMapperConfig.CreateMapper();
}
Код: Выделить всё
namespace SoldierTrack.Services.Common
{
using AutoMapper;
public static class AutoMapperConfig
where T : Profile, new()
{
public static IMapper CreateMapper()
{
var mapConfiguration = new MapperConfiguration(config =>
{
config.AddProfile();
});
return mapConfiguration.CreateMapper();
}
}
}
Я попробовал это:
Я реорганизовал AutoMapperConfig следующим образом:
Код: Выделить всё
public static class AutoMapperConfig
{
public static IMapper Initialize()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile();
cfg.AddProfile();
cfg.AddProfile();
cfg.AddProfile();
});
return config.CreateMapper();
}
}
Код: Выделить всё
public class MembershipService
{
private readonly ApplicationDbContext data;
private readonly IMapper mapper;
public MembershipService(ApplicationDbContext data, IMapper mapper)
{
this.data = data;
this.mapper = mapper;
}
}
Код: Выделить всё
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
var mapper = AutoMapperConfig.Initialize();
services.AddSingleton(mapper); //I have added this two lines
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
return services;
}
Код: Выделить всё
An unhandled exception occurred while processing the request.
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
CreateWorkoutViewModel -> WorkoutServiceModel
SoldierTrack.Web.Areas.Administrator.Models.Workout.CreateWorkoutViewModel -> SoldierTrack.Services.Workout.Models.WorkoutServiceModel
Код: Выделить всё
builder.Services.AddAutoMapper(typeof(Program).Assembly);
Итак, я вернул свой код, как в исходном состоянии.
Подробнее здесь: https://stackoverflow.com/questions/790 ... onstructor