title: Circular dependency in ASP.NET Core
body: <p>For school we have to write our own WebApi using the .NET Entity Core Framework. I've written my api but when I tried to use it in swagger, it always returned a HTTP 500 error: internal server error. I downloaded Fiddler to start debugging and came across a circular dependency error in my repository but I can't figure out where this would take place.</p>
  
  <p>The interface (for mock testing)</p>
  
  <pre><code>public interface IVisitorRepository
  {
      Visitor GetBy(string email);
      void AddVisitor(Visitor visitor);
      void SaveChanges();
  }
  </code></pre>
  
  <p>The concrete class</p>
  
  <pre><code>public class VisitorRepository : IVisitorRepository 
  {
      private readonly ApplicationDbContext _context;
      private readonly DbSet&lt;Visitor&gt; _visitors;
  
      public VisitorRepository(ApplicationDbContext context, IVisitorRepository visitorRepository) 
      {
          _context = context;
          _visitors = _context.Visitors;
      }
  
      public void AddVisitor(Visitor visitor) 
      {
          _visitors.Add(visitor);
      }
  
      public Visitor GetBy(string email) 
      {
          return _visitors.SingleOrDefault(v =&gt; v.Email == email);
      }
  
      public void SaveChanges() 
      {
          _context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>I've scoped it in my pipeline. </p>
  
  <p>It's a JWT token based login and register api (that's what we need to make) and here's my register method (the method I'm testing)</p>
  
  <pre><code>    [AllowAnonymous]
      [HttpPost("register")]
      public async Task&lt;ActionResult&lt;String&gt;&gt; Register(RegisterDTO model) 
      {
          IdentityUser user = new IdentityUser { UserName = model.Email, Email = model.Email };
          Visitor visitor = new Visitor(model.FirstName + " " + model.LastName, model.Email, model.PhoneNumber, model.Country);
          var result = await _userManager.CreateAsync(user, model.Password);
  
          if (result.Succeeded) 
          {
              _visitorRepository.AddVisitor(visitor);
              _visitorRepository.SaveChanges();
              string token = GetToken(user);
              return Created("", token);
          }
          return BadRequest();
      }
  </code></pre>
  
  <p>The exception:</p>
  
  <blockquote>
    <p>InvalidOperationException: A circular dependency was detected for the service of type 'DigitizedApi.Models.Repositories.IVisitorRepository'. DigitizedApi.Models.Repositories.IVisitorRepository(DigitizedApi.Data.Repositories.VisitorRepository) -> DigitizedApi.Models.Repositories.IVisitorRepository</p>
  </blockquote>