1

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.

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.