I have an MVC project with makes use of the DI pattern in my controllers, using Unity to resolve my dependencies.
I recently introduced WebApi package and it is after introducing an annoying bug.
When I try to inject my service class into a web api controller I get the following error:
"An error occurred when trying to create a controller of type 'TestController'. Make sure that the controller has a parameterless public constructor."
This is the code
public class TestController : ApiController
{
private readonly IBrokerService _brokerAccessService;
public TestController(IBrokerService brokerAccessService)
{
_brokerAccessService = brokerAccessService;
}
/// Controller methods here
}
I can add a paramaterless constructor like so and it works fine
public TestController()
{
_brokerAccessService = new BrokerService(new BrokerRepository(new ForexRebatesNowEntities()));
}
But that totally violates the DI pattern
I have this Service injected into other controllers (derived from MVCs 'BaseController' rather than the WebApi 'ApiController') and there are no issues
Is there a difference in the way MVC controllers and APi Controllers handle DI?