Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Just wondering if anyone has had any exposure to using Ninject to inject services in to an UmbracoApiController. I have a standard out of the box Ninject setup at the moment, and when hitting my API url I get the following:
An error occurred when trying to create a controller of type 'RandomApiController'. Make sure that the controller has a parameterless public constructor.
I am aware that adding a parameterless constructor will resolve this exception, but obviously I need the constructor injection to take place.
public class RandomApiController : UmbracoApiController
private readonly IRandomProvider _service;
public RandomApiController(IRandomProvider service)
this._service = service;
public IEnumerable<RandomModel> Search(string serach)
Managed to find a resolution: http://stackoverflow.com/questions/11652862/ninject-and-asp-net-web-api
Turns out WebApi requires an explicitly set DependencyResolver which is assigned to GlobalConfiguration.Configuration.DependencyResolver
We used ninject on an umbraco project, we used the https://www.nuget.org/packages/Ninject.Web.WebApi/ nuget package that wired everything up for us. One thing that was an issue was injecting in Umbraco services because it was too early in the pipeline, however we had
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
And that called
/// this is called from UmbracoEvents.OnApplicationStarted have to do this else we get errors due to
/// having no umbraco context if we done from OnApplicationStarted then we do have context and it all works
public static void UmbracoStart()
For reference - this works better than my solutions as my solution would replace Umbraco's GlobalDependencyResolver.
I added reference for Ninject.Web.WebApi. But I can't recognize CreateKernel variable in UmbracoStart() method
When you say you added in reference you mean via nuget? If so then it should have added file NinjectWebCommon.cs in App_start open that file you should have CreateKernel method there? Or you need to create it to wireup everything mine looks like:
/// Creates the kernel that will manage your application.
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted