.NET Core - Trying to add a repository to my API controller, but when I do every controller method returns a 500 error

好久不见. 提交于 2019-12-08 01:01:54

问题


As the title says. I'm creating a Web API and in my API controller, I'm trying to declare a repository in the constructor. I successfully declare it, but every API method I try to call in that controller returns a 500 error. When I remove the constructor/repository variable, I have no issues.

Controller

[Route("api/[controller]")]
public class TestController: Controller
{
   private ITestRepository _testRepository;

   public TestController(ITestRepository testRepository)
   {
       _testRepository= testRepository;
   }

   [HttpGet]
   public IEnumerable<string> Get()
   {
       return new string[] { "value1", "value2" };
   }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   // Add framework services.
   services
      .AddMvcCore()
      .AddJsonFormatters()
      .AddApiExplorer();

   services.AddScoped<ITestRepository , TestRepository >();    
   services.AddSwaggerGen();
}

Am I missing something?


回答1:


Short Answer

I'm trying to declare a repository in the constructor. I successfully declare it, but every API method I try to call in that controller returns a 500 error. When I remove the constructor/repository variable, I have no issues.

You probably need to make one of two changes:

  1. remove the parameters from the repository's constructor, or
  2. register the services that the repository's constructor takes.

Explanation

The exact code from your question works with the following repository code.

public interface ITestRepository { }

public class TestRepository : ITestRepository { }

The code throws a 500 error, though, if the constructor takes a parameter.

public class TestRepository : ITestRepository
{
    public TestRepository(object someObject)
    {

    }
}

It throws with that constructor, because a call to services.AddScoped<ITestRepository, TestRepository>() requires that the TestRepository constructor meets one of these two criteria.

  1. a constructor without parameters, or
  2. a constructor that takes resolvable services.

So to fix your code you need to make one of two changes:

  1. remove the parameters from the constructor, or
  2. register the services that your constructor takes.

For instance, if the repository takes a DbContext in its constructor, then your code might look like this.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddJsonFormatters()
        .AddApiExplorer();

    services
        .AddEntityFramework()
        .AddInMemoryDatabase()
        .AddDbContext<TestDbContext>(); // register a service

    services.AddScoped<ITestRepository, TestRepository>();
    services.AddSwaggerGen();
}

TestRepository.cs

public class TestRepository : ITestRepository
{
    // pass the registered service to the ctor
    public TestRepository(TestDbContext testDbContext)
    {

    }
}



回答2:


First we register the dependent component using Microsoft.practices.Unity, and second we resolve them where we are to use them.

You have not resolved your dependency before using it.

public class TestController: Controller
{
    private ITestRepository _testRepository;

    public TestController(ITestRepository testRepository)
    {
        _testRepository= testRepository;
    }

    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

Registering here:

DIContainer.Instance.RegisterType<ITagManager, TagManager>();

We resolve our dependencies before using them.

DIContainer.Instance.Resolve<ITagManager>().RetrieveTwitterTags();


来源:https://stackoverflow.com/questions/36778658/net-core-trying-to-add-a-repository-to-my-api-controller-but-when-i-do-every

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!