title: ASP.NET Core 2.2: Unable to resolve service for type 'AutoMapper.IMapper'
body: <p><strong><em>ASP.NET Core (Version: 2.2.102)</em></strong></p>
  
  <p>I am building an API to return Portos and Especies, but anytime that I access /api/portos (as defined in the controller), I get this error:</p>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service for type
    'AutoMapper.IMapper' while attempting to activate
    'fish.Controllers.PortosController'.</p>
    
    <p>Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
    sp, Type type, Type requiredBy, bool isDefaultParameterRequired)</p>
  </blockquote>
  
  <p>I am not sure what am I doing wrong, so any help is appreciated.</p>
  
  <hr>
  
  <p><strong>Models</strong></p>
  
  <hr>
  
  <p><em>Especie.cs</em></p>
  
  <pre><code>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; }
      }
  }
  </code></pre>
  
  <p><em>Porto.cs</em></p>
  
  <pre><code>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&lt;Especie&gt; Models { get; set; }
  
          public Porto()
          {
              Models = new Collection&lt;Especie&gt;();
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Controller</strong></p>
  
  <hr>
  
  <p><em>PortoController.cs</em></p>
  
  <pre><code>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&lt;IEnumerable&lt;PortoResource&gt;&gt; GetPortos()
          {
              var portos = await context.Portos.Include(m =&gt; m.Models).ToListAsync();
  
              return mapper.Map&lt;List&lt;Porto&gt;, List&lt;PortoResource&gt;&gt;(portos);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Controller>Resources</strong></p>
  
  <p><em>PortoResources.cs</em></p>
  
  <pre><code>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&lt;EspecieResource&gt; Models { get; set; }
  
          public PortoResource()
          {
              Models = new Collection&lt;EspecieResource&gt;();
          }
      }
  }
  </code></pre>
  
  <p><em>EspecieResource.cs</em></p>
  
  <pre><code>namespace fish.Controllers.Resources
  {
      public class EspecieResource
      {
          public int Id { get; set; }
          public string Nome { get; set; }
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>More Relevant Code</strong></p>
  
  <hr>
  
  <p><em>Stratup.cs</em></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
          services.AddAutoMapper();
  
          services.AddDbContext&lt;FishDbContext&gt;(options =&gt; 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 =&gt;
          {
              configuration.RootPath = "ClientApp/dist";
          });
  }
  </code></pre>
  
  <p><em>MappingProfile.cs</em></p>
  
  <pre><code>using AutoMapper;
  using fish.Controllers.Resources;
  using fish.Models;
  
  namespace fish.Mapping
  {
      public class MappingProfile : Profile
      {
          public MappingProfile()
          {
              CreateMap&lt;Porto, PortoResource&gt;();
              CreateMap&lt;Especie, EspecieResource&gt;();
          }
      }
  }
  </code></pre>
  
  <p><em>FishDbContext.cs</em></p>
  
  <pre><code>using fish.Models;
  using Microsoft.EntityFrameworkCore;
  
  namespace fish.Persistence
  {
      public class FishDbContext : DbContext
      {
          public FishDbContext(DbContextOptions&lt;FishDbContext&gt; options) : base(options)
          {
  
          }
  
          public DbSet&lt;Porto&gt; Portos { get; set; }
      }
  }
  </code></pre>