Reputation: 2701
In my MVC5 application I have used Unity.Mvc4 from NuGet to manage my dependency injection.
This creates Bootstrapper.cs
which looks like following:
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IUnitOfWork, ApplicationDbContext>();
}
}
Afterwards I initialize Unity under Global.asax.cs
like following:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Bootstrapper.Initialise(); // < ----- here
}
My regular Controller
(e.g. HomeController) has no issues with injecting anything into it's constructor. However once I try to inject anything in my ApiController
which is located in the same project like this:
public class ValuesController : ApiController
{
private readonly IUnitOfWork _repository;
public ValuesController(IUnitOfWork repository)
{
_repository = repository;
}
public IEnumerable<PatientData> Get()
{
return _repository.Repository.ReturnResults();
}
public HttpResponseMessage Get(string id)
{
var patient = _repository.Repository.FindById(Convert.ToInt32(id));
return Request.CreateResponse(HttpStatusCode.OK, patient);
}
}
and I try to call simple GET request to this constructor I suddenly receive the following error message:
An error occurred when trying to create a controller of type 'ValuesController'. Make sure that the controller has a parameterless public constructor.
I know there is something I need to do something with DependencyResolver under my WebApiConfig
, but I am not quite sure what. Here is how my class looks like now:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Any help / suggestions in this matter would be more than appreciated.
Upvotes: 0
Views: 588
Reputation: 15663
You have configured only the MVC dependency resolver, you need to do it also for the Web API framework too. Both frameworks have their own dependency injection configurations.
Add this in the Initialise
method
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
new Unity.WebApi.UnityDependencyResolver(container);
return container;
}
You also need to install the Unity.WebAPI
package
Upvotes: 3