2

I am injecting a dependency into an custom Api controller I've created that is inheriting from ServicesApiController. I have found that I need to use our service locator class to do this as otherwise I get an error saying 'Make sure that the controller has a parameterless public constructor'. Here is some example code:

   public class CustomApiController : ServicesApiController
        {
            private readonly ISomeDependancy _someDependancy;

            public CustomApiController() : this(LocatorContainerManager.Current.Resolve<ISomeDependancy>())
            {

            }

            public CustomApiController(ISomeDependancy someDependancy)
            {
                _someDependancy = someDependancy;
            }

            [HttpGet]
            public IHttpActionResult Get()
            {
               //return some items here
            }
    }

Obviously I'd rather not use ServiceLocator. The custom routes are patched in replacing Sitecores (Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc)

We are using SimpleInjector and as I understand it the problem might be that the configuration is being overwritten by Sitecore as the custom routes are patched in before the ServicesWebApiInitializer.

Therefore I may need to move where they are registered to after the WebApiInitializer as explained here by Corey:

WebApi failing to resolve dependencies

Does this sound right or am I miss-understanding something?

Further info: Sitecore 8.2 Update 6. We are using xDB.

  • Did you ever resolve this? – Mark Cassidy Jan 9 at 12:50
  • I don't think so unfortunately Mark, I think I just ended up using service locator in the end :-( – Adam Seabridge Jan 10 at 9:31
  • The problem exists on a 9.2 as well. And everything is patched in via <services> configuration element and so on. – Mark Cassidy Jan 10 at 9:58
  • Right ok, thats interesting. If you find a fix I'd be interested to know what it is. – Adam Seabridge Jan 10 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.