3

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.

The interface (for mock testing)

public interface IVisitorRepository
{
    Visitor GetBy(string email);
    void AddVisitor(Visitor visitor);
    void SaveChanges();
}

The concrete class

public class VisitorRepository : IVisitorRepository 
{
    private readonly ApplicationDbContext _context;
    private readonly DbSet<Visitor> _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 => v.Email == email);
    }

    public void SaveChanges() 
    {
        _context.SaveChanges();
    }
}

I've scoped it in my pipeline.

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):

[AllowAnonymous]
[HttpPost("register")]
public async Task<ActionResult<String>> 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();
}

The exception:

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

2
  • 6
    Your VisitorRepository (which implements IVisitorRepository) has a dependency on IVisitorRepository (itself). Check the constructor. Commented Mar 17, 2019 at 11:01
  • @FedericoDipuma wow that was dumb, Thanks a lot, that fixed it! Commented Mar 17, 2019 at 11:06

1 Answer 1

6

Problem is your VisitorRepository (which implements IVisitorRepository) has a dependency on IVisitorRepository itself.

Actually it should be as follows:

public class VisitorRepository : IVisitorRepository 
{
    private readonly ApplicationDbContext _context;
    private readonly DbSet<Visitor> _visitors;

    public VisitorRepository(ApplicationDbContext context) 
    {
        _context = context;
        _visitors = _context.Visitors;
    }
    .........
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.