Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kieron McIntyre 117 posts 360 karma points
    Sep 11, 2013 @ 12:31
    Kieron McIntyre
    0

    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:

    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.

    Any ideas?

  • Kieron McIntyre 117 posts 360 karma points
    Sep 30, 2013 @ 18:24
Please Sign in or register to post replies

Write your reply to:

Draft