I have a class that inherits from the UmbracoApiController class and I cannot get my dependencies injected into it:
public class EnquiriesApiController : UmbracoApiController
{
private readonly IEnquiryService _enquiryService;
public EnquiriesApiController(IEnquiryService enquiryService)
{
_enquiryService = enquiryService;
}
public void Post([FromBody]Enquiry model)
{
enquiryService.SubmitEnquiry(model);
}
}
I am using the standard AutofacWebApiDependencyResolver set up, described here (I am already using AutofacDependencyResolver for my standard controller registration).
If I remove the constructor, the EnquiriesApiController loads fine but obviously the _enquiryService will never be set. When I add it back in, the constructor is never hit, and my action is also never hit either.
I have checked the Umbraco source and I can see that the UmbracoApiController has a constructor that requires an UmbracoContext parameter, so I assume this is why my constructor and action may never get hit.
So, the question is how do I DI into my class? I have tried property injection and this doesn't work either.
How to DI into UmbracoApiController subclasses?
I have a class that inherits from the
UmbracoApiController
class and I cannot get my dependencies injected into it:I am using the standard
AutofacWebApiDependencyResolver
set up, described here (I am already using AutofacDependencyResolver for my standard controller registration).If I remove the constructor, the
EnquiriesApiController
loads fine but obviously the_enquiryService
will never be set. When I add it back in, the constructor is never hit, and my action is also never hit either.I have checked the Umbraco source and I can see that the
UmbracoApiController
has a constructor that requires anUmbracoContext
parameter, so I assume this is why my constructor and action may never get hit.So, the question is how do I DI into my class? I have tried property injection and this doesn't work either.
Any ideas?
Actually this does work. I started again and took notes from the written here:
http://our.umbraco.org/forum/developers/api-questions/41794-Web-API-UmbracoApiController-does-not-use-DependencyResolver#comment153127
is working on a reply...