Copied to clipboard

Flag this post as spam?

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


  • Gary Devenay 39 posts 245 karma points
    Jul 30, 2015 @ 11:14
    Gary Devenay
    0

    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:

    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);
        }
    }
    
  • Gary Devenay 39 posts 245 karma points
    Jul 30, 2015 @ 12:13
    Gary Devenay
    1

    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

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 30, 2015 @ 12:52
    Ismail Mayat
    101

    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

    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);
        }
    

    Regards

    Ismail

  • Gary Devenay 39 posts 245 karma points
    Aug 05, 2015 @ 13:34
    Gary Devenay
    0

    Cheers Ismail,

    For reference - this works better than my solutions as my solution would replace Umbraco's GlobalDependencyResolver.

  • Nguyen Hien 52 posts 133 karma points
    Jan 17, 2016 @ 13:55
    Nguyen Hien
    0

    I added reference for Ninject.Web.WebApi. But I can't recognize CreateKernel variable in UmbracoStart() method

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 18, 2016 @ 13:47
    Ismail Mayat
    0

    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;
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft