title: ASP.NET Core Dependency Injection Issues
body: <p>I have a mini ASP.NET project where I am trying to use the <code>Repository/UnitOfWork</code> architecture principle. Everything should be working as it should, however, I'm constantly encountering an issue with the dependency within my <code>StartUp</code> file. I'm not sure where I'm going wrong here.  Any help is very much appreciated.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>An unhandled exception occurred while processing the request.
        InvalidOperationException: Unable to resolve service for type 'Project.UOW.IUnitOfWork' while attempting to activate 'Project.Controllers.UsersController'.</p>
  </blockquote>
  
  <p>The above error only happens when I inject IUnitOfWork as a dependency in the Startup file. If I add all three dependencies, I get a different error in the effect of =></p>
  
  <p>"HTTP Error 502.3 - Bad Gateway. The specified CGI application encountered an error and the server terminated the process."    </p>
  
  <pre><code>Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
  </code></pre>
  
  <p>IRepository:</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class 
  {
      Task&lt;TEntity&gt; GetById(int id);
      Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetAll();
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
          private readonly DbContext _context;
          public Repository(DbContext context)
          {
              _context = context;
          }
  
          public async Task&lt;TEntity&gt; GetById(int id)
          {
              return await _context.Set&lt;TEntity&gt;().FindAsync(id);
          }
  
          public async Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetAll()
          {
              return await _context.Set&lt;TEntity&gt;().ToListAsync();
          }
  }
  </code></pre>
  
  <p>IUserRepository:</p>
  
  <pre><code>  public interface IUserRepository : IRepository&lt;User&gt;
    {
          Task&lt;IEnumerable&lt;User&gt;&gt; GetAllUsers();
          IEnumerable&lt;User&gt; GetUserById(int Id);
    }
  </code></pre>
  
  <p>UserRepository:</p>
  
  <pre><code>public class UserRepository : Repository&lt;User&gt;, IUserRepository
  {
  
      public UserRepository(DbContext context)
          : base(context)
      {
  
      }
  
      public IEnumerable&lt;User&gt; GetUserById(int Id)
      {
          return Context.User.Where(p =&gt; p.UserId == Id).ToList();
      }
  
      public async Task&lt;IEnumerable&lt;User&gt;&gt; GetAllUsers()
      {
          return await Context.User.ToListAsync();
      }
  }
  </code></pre>
  
  <p>UnitOfWork:</p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
          private DbContext _context;
  
          public UnitOfWork(DbContext context)
          {
              _context = context;
              User = new UserRepository(_context); 
          }
  
          public IUserRepository User { get; set; }
  
          public int Complete()
          {
              return _context.SaveChanges();
          }
  
          public void Dispose()
          {
              _context.Dispose();
          }
  </code></pre>
  
  <p>}</p>
  
  <p>IUnitOfWork:</p>
  
  <pre><code> public interface IUnitOfWork : IDisposable
   {
          IUserRepository User { get; set; }
          int Complete();
   }
  </code></pre>
  
  <p>Startup:</p>
  
  <pre><code>services.AddScoped&lt;IUnitOfWork, UnitOfWork&gt;();
  services.AddTransient&lt;IUserRepository, UserRepository&gt;();
  services.AddTransient(typeof(IRepository&lt;&gt;), typeof(Repository&lt;&gt;));
  </code></pre>
  
  <p>UserController:</p>
  
  <pre><code> public class UsersController : Controller
   {
          private IUnitOfWork _unitOfWork;
  
          public UsersController(IUnitOfWork unitOfWork)
          {
              _unitOfWork = unitOfWork;
          }
  
          // GET: Users
          public async Task&lt;IActionResult&gt; Index()
          {
              var getAllUsers = _unitOfWork.User.GetAllUsers();
  
              return View(await getAllUsers);
          }
  }
  </code></pre>