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;
}
[HttpGet]
public IEnumerable<RandomModel> Search(string serach)
{
return this._service.Search(input);
}
}
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)
{
NinjectWebCommon.UmbracoStart();
}
And that called
/// <summary>
/// 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
/// </summary>
public static void UmbracoStart()
{
Bootstrapper.Initialize(CreateKernel);
}
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:
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
//webapi injection
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
Ninject with UmbracoApiController
Hey all,
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:
I am aware that adding a parameterless constructor will resolve this exception, but obviously I need the constructor injection to take place.
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
Gary,
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
And that called
Regards
Ismail
Cheers Ismail,
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:
is working on a reply...