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?
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:
- remove the parameters from the repository's constructor, or
- 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.
- a constructor without parameters, or
- a constructor that takes resolvable services.
So to fix your code you need to make one of two changes:
- remove the parameters from the constructor, or
- 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)
{
}
}
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