title: System.InvalidOperationException: Unable to resolve service for type
body: <p>I'm working on a Web API with ASP.NET Core. When I'm executing my API with a post request, an exception is throwing before my break point in the Post method of <em>UnitController</em>.</p>
  
  <p><strong>Exception</strong></p>
  
  <blockquote>
    <p>Request starting HTTP/1.1 POST <a href="http://localhost:5000/api/unit" rel="noreferrer">http://localhost:5000/api/unit</a>
    application/json 31 fail: Microsoft.AspNetCore.Server.Kestrel[13]
          Connection id "0HKVTL9A1LTD4": An unhandled exception was thrown by the application. System.InvalidOperationException: Unable to resolve service for type 'Project.DataAccess.Repository.UnitRepository' while attempting to activate 'Project.Service.UnitService'.</p>
  </blockquote>
  
  <p><strong>Context</strong></p>
  
  <pre><code>namespace Project.DataAccess.Library.Interface {
  public interface IBaseRepository&lt;M&gt; where M : class, IEntity
  {
      IEnumerable&lt;M&gt; SelectAll();
  
      M SelectByID(int id);
  
      void Insert(M obj);
  
      void Update(M obj);
  
      void Delete(int id);
  
      void Save();
  }
  }
  
  namespace Project.DataAccess.Library {
  public abstract class BaseRepository&lt;M&gt; : IBaseRepository&lt;M&gt; where M : class, IEntity
  {
      protected ProjectContext Db { get; }
      private DbSet&lt;M&gt; table = null;
  
      protected DbSet&lt;M&gt; Table
      {
          get
          {
              return this.table;
          }
      }
  
      public BaseRepository(ProjectContext dbContext)
      {
          Db = dbContext;
          this.table = Db.Set&lt;M&gt;();
      }
  
      public void Delete(int id)
      {
          M existing = this.SelectByID(id);
          if (existing != null)
              this.table.Remove(existing);
      }
  
      // others methods
  }
  }
  
  namespace Project.DataAccess.Repository
  {
      public class UnitRepository : BaseRepository&lt;Unit&gt;, IUnitRepository
      {
          public UnitRepository(Projectcontext) : base(context) { }
      }
  }
  
  namespace Project.Service
  {
      public class UnitService : BaseService&lt;Unit&gt;, IUnitService
      {
          public UnitService(UnitRepository unitRepository) : base(unitRepository) { }
      }
  }
  
  
  namespace AssoManager.Service.Library
  {
      public abstract class BaseService&lt;M&gt; : IBaseService&lt;M&gt; where M : class, IEntity
      {
          private IBaseRepository&lt;M&gt; _repository;
  
          public BaseService(IBaseRepository&lt;M&gt; repository)
          {
              _repository = repository;
          }
  
          public IEnumerable&lt;M&gt; GetAll()
          {
              return this._repository.SelectAll();
          }
      }
   }
  
  
  namespace Project
  {
      public class Startup
      {
          public Startup(IHostingEnvironment env)
          {
              var builder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                  .AddEnvironmentVariables();
              Configuration = builder.Build();
          }
  
          public IConfigurationRoot Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              // Add framework services.
              services.AddMvc();
  
              this.DataAccessMySqlConfiguration(services);
              this.ConfigureRepository(services);
              this.ConfigureServicesUnit(services);
              this.ConfigureServicesUser(services);
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
              loggerFactory.AddConsole(Configuration.GetSection("Logging"));
              loggerFactory.AddDebug();
  
              app.UseMvc();
          }
  
          #region Database configuration
  
          public void DataAccessMySqlConfiguration(IServiceCollection services)
          {
              services.AddDbContext&lt;ProjectContext&gt;(options =&gt; options.UseMySQL(Configuration.GetConnectionString("MsSQLConnection")));
          }
  
          #endregion
  
          #region DataAccess configuration
  
          public void ConfigureRepository(IServiceCollection services)
          {
              services.AddScoped&lt;IUnitRepository, UnitRepository&gt;();
              services.AddScoped&lt;IUserRepository, UserRepository&gt;();
          }
  
          #endregion
  
          #region Services configuration
  
          /// &lt;summary&gt;
          /// Is used to add unit services to the container
          /// &lt;/summary&gt;
          public void ConfigureServicesUnit(IServiceCollection services)
          {
              services.AddTransient&lt;IUnitService, UnitService&gt;();
              services.AddTransient&lt;IMeetingService, MeetingService&gt;();
          }
  
          /// &lt;summary&gt;
          /// Is used to add user services to the container
          /// &lt;/summary&gt;
          public void ConfigureServicesUser(IServiceCollection services)
          {
              services.AddTransient&lt;IUserService, UserService&gt;();
          }
  
          #endregion Services configuration
      }
  }
  
  
  namespace Project.Controllers
  {
      [Route("api/[controller]")]
      public class UnitController : Controller
      {
          private IUnitService UnitService;
  
          public UnitController(IUnitService unitService)
          {
              UnitService = unitService;
          }
  
          // GET api/units
          [HttpGet]
          public IEnumerable&lt;Unit&gt; Get()
          {
              return UnitService.GetAll();
          }
  
          // GET api/unit/5
          [HttpGet("{id}")]
          public IActionResult Get(int id)
          {
              Unit unit;
              //-&gt;Check
              if (id &lt; 1)
                  return BadRequest();
              //-&gt;Processing
              unit = UnitService.GetByID(id);
              if (unit == null)
                  return NotFound();
              return new ObjectResult(unit);
          }
  
          // POST api/unit
          [HttpPost]
          public IActionResult Post([FromBody]Unit unit)
          {
              //-&gt;Check
              if (unit == null)
                  return BadRequest();
              //-&gt;Processing
              UnitService.Create(unit);
              return CreatedAtRoute("Get", new { id = unit.Id }, unit);
          }
  
          // PUT api/unit/5
          [HttpPut("{id}")]
          public void Put(int id, [FromBody]string value)
          {
          }
  
          // DELETE api/unit/5
          [HttpDelete("{id}")]
          public void Delete(int id)
          {
          }
      }
  }
  </code></pre>
  
  <p><strong>Version</strong></p>
  
  <pre><code>"dependencies": {
      "Microsoft.NETCore.App": {
        "version": "1.0.0",
        "type": "platform"
      },
      "Microsoft.AspNetCore.Mvc": "1.0.0",
      "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
      "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
      "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
      "Microsoft.Extensions.Configuration.Json": "1.0.0",
      "Microsoft.Extensions.Logging": "1.0.0",
      "Microsoft.Extensions.Logging.Console": "1.0.0",
      "Microsoft.Extensions.Logging.Debug": "1.0.0",
      "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
      "Microsoft.EntityFrameworkCore": "1.0.0",
      "MySql.Data.Core": "7.0.4-IR-191",
      "MySql.Data.EntityFrameworkCore": "7.0.4-IR-191",
      "IdentityServer4": "1.0.0-rc2",
      "AssoManager.Domain": "1.0.0-*",
      "AssoManager.Service": "1.0.0-*",
      "AssoManager.DataAccess": "1.0.0-*"
    },
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>I think the problem is maybe with the inheritance between BaseRepository and IBaseRepository. But I don't understand where could be my mistake.
  How can I correct this bug ?</p>
  
  <p>Thank you for your helps :),</p>