Copied to clipboard

Flag this post as spam?

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


  • Dave de Moel 122 posts 574 karma points c-trib
    Sep 08, 2016 @ 07:17
    Dave de Moel
    0

    Problem with using Ninject as IoC container

    Hi guys and gals,

    I am having a problem with Umbraco and NInject to which I hope you people can be of help.

    Currently I am trying to seperate out business layer of code from the Umbraco CMS frontend due to customer demands, and we have decided to use Ninject as the IoC container, due to previous experience (outside of Umbraco) and it's ease of use (and lack of XML).

    To install it I have followed the steps provided in the following tutorial: Integrating an application into Umbraco (Using Ninject)

    Sadly I ran into an issue. At first it didn't work at all, I just kept getting the message that a controller couldn't be instantiated without a parameterless constructor. So after a bunch of searching around, I found that I apparantly need to set the dependency resolver as mentioned in the Umbraco docs here: Docs

    That does make Ninject work, however, now I am having the issue that I am getting kicked out of the backoffice as soon as I log in :(

    I made a gist of all the used code here: Gist

    I personally can't find the problem. If I comment out the setting of the dependency resolver the backoffice works but the injection doesn't and if I set the resolver then I keep getting logged out. Now I noticed that with Autofac apparantly you need to register the activator with the IoC container, but I have not seen a way to do that with Ninject. Can anyone help?

  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    Sep 08, 2016 @ 08:48
    Darren Ferguson
    1

    Did you do this bit from the article....

    Just one more thing:

    If you want to use Ninject to inject Umbraco API controllers - there is a little trick. I can't claim any credit for this trick it comes courtesy of the following post on the our.umbraco forum.

    In application startup call the following code:

    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new UmbracoWebApiHttpControllerActivator(kernel)); The implementation of UmbracoWebApiHttpControllerActivator is provided in the post - along with a discussion of why it is needed.

    https://our.umbraco.org/forum/core/general/55057-714-WebApi-and-Unity-IOC-brakes-BackOffice

  • Dave de Moel 122 posts 574 karma points c-trib
    Sep 08, 2016 @ 09:00
    Dave de Moel
    0

    Yeah I did. But in your article it seems like you have some custom implementation for it (you pass the kernel as a parameter), but that implementation is not posted, so I have an exact copy of the one from the original thread.

    You can check the gist in the OP for the exact code I used to set up Ninject.

  • Darren Ferguson 1022 posts 3259 karma points MVP c-trib
    Sep 08, 2016 @ 09:05
    Darren Ferguson
    104

    See:

    https://gist.github.com/anonymous/8775ac57c35faaa4df3d04178c7ca7b6

    Registered with:

    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new UmbracoWebApiHttpControllerActivator(kernel));

  • Dave de Moel 122 posts 574 karma points c-trib
    Sep 08, 2016 @ 09:15
    Dave de Moel
    0

    you sir, are a hero. I'll try it out as soon as I am able.

    Edit: It works :D I'd say update your original article, as I am sure I am not the only one running into this problem.

    Thanks thanks thanks :D

  • Jules 274 posts 585 karma points
    Apr 29, 2017 @ 14:20
    Jules
    0

    Had same issue. Sorted now with Darren's post above

Please Sign in or register to post replies

Write your reply to:

Draft