ASP.NET Core (версия: 2.2.102)
Я создаю API для возврата Portos и Especies, но каждый раз, когда я обращаюсь к /api/portos (как определено в контроллере), я получаю эту ошибку:
InvalidOperationException: Не удалось разрешить службу для типа
«AutoMapper.IMapper» при попытке активировать
«fish.Controllers.PortosController».
Microsoft.Extensions. DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
sp, Тип типа, Тип требуется, bool isDefaultParameterRequired)
Я не уверен, что я делаю неправильно, поэтому буду благодарен за любую помощь.
Модели
Especie.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace fish.Models
{
[Table("Especies")]
public class Especie
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Nome { get; set; }
public Porto Porto { get; set; }
public int PortoId { get; set; }
}
}
Porto.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
namespace fish.Models
{
public class Porto
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Nome { get; set; }
public ICollection Models { get; set; }
public Porto()
{
Models = new Collection();
}
}
}
Контроллер
PortoController.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using fish.Controllers.Resources;
using fish.Models;
using fish.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace fish.Controllers
{
public class PortosController : Controller
{
private readonly FishDbContext context;
private readonly IMapper mapper;
public PortosController(FishDbContext context, IMapper mapper)
{
this.mapper = mapper;
this.context = context;
}
[HttpGet("/api/portos")]
public async Task GetPortos()
{
var portos = await context.Portos.Include(m => m.Models).ToListAsync();
return mapper.Map(portos);
}
}
}
Контроллер>Ресурсы
PortoResources.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace fish.Controllers.Resources
{
public class PortoResource
{
public int Id { get; set; }
public string Nome { get; set; }
public ICollection Models { get; set; }
public PortoResource()
{
Models = new Collection();
}
}
}
EspecieResource.cs
namespace fish.Controllers.Resources
{
public class EspecieResource
{
public int Id { get; set; }
public string Nome { get; set; }
}
}
Более релевантный код
< hr />
Stratup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper();
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
MappingProfile.cs
using AutoMapper;
using fish.Controllers.Resources;
using fish.Models;
namespace fish.Mapping
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap
();
CreateMap();
}
}
}
FishDbContext.cs
using fish.Models;
using Microsoft.EntityFrameworkCore;
namespace fish.Persistence
{
public class FishDbContext : DbContext
{
public FishDbContext(DbContextOptions options) : base(options)
{
}
public DbSet Portos { get; set; }
}
}
Подробнее здесь: https://stackoverflow.com/questions/542 ... er-imapper