+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Ninject MVC with Sitefinity

Ninject MVC with Sitefinity

18 posts, 1 answered
  1. Narasimha
    Narasimha avatar
    14 posts
    Registered:
    26 May 2015
    09 Jun 2015
    Link to this post

    I have created a Sitefinity MVC4 project, however when I try to use Ninject MVC4 to use IOC, I get the following error. The same example works fine in a regular MVC project

     

    Error activating ICommonFacadeWrapper
    No matching bindings are available, and the type is not self-bindable.
    Activation path:
      2) Injection of dependency ICommonFacadeWrapper into parameter wrapper of constructor of type HomeController
      1) Request for HomeController

    Suggestions:
      1) Ensure that you have defined a binding for ICommonFacadeWrapper.
      2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
      3) Ensure you have not accidentally created more than one kernel.
      4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
      5) If you are using automatic module loading, ensure the search path and filters are correct.​

  2. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Jun 2015
    Link to this post
    Hi Narasimha,

    You can check how to use Ninject with Sitefinity here.

    I have created an MVC widget controller using a service loaded with Ninject.
    I have the following service and controller:
    Service:
    Copy Code
    public class NewsItemModelsService : INewsItemModelsService
    {
    ...
    }

    Controller:
    Copy Code
    [Inject]
    public NewsWidgetCustomController(INewsItemModelsService modelsService)
    {
        this.ModelsService = modelsService;
    }

    Ninject factory:
    Copy Code
    using Ninject;
    using Ninject.Modules;
    using SitefinityWebApp.Mvc.Models;
    using System;
    using System.Web.Mvc;
    using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;
     
    namespace SitefinityWebApp.Mvc
    {
        public class NinjectControllerFactory : FrontendControllerFactory
        {
            private IKernel ninjectKernel;
     
            /// <summary>
            /// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class.
            /// </summary>
            public NinjectControllerFactory()
            {
                this.ninjectKernel = new StandardKernel(new InterfaceMappings());
            }
     
            protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
            {
                if (controllerType == null)
                {
                    return null;
                }
     
                var resolvedController = this.ninjectKernel.Get(controllerType);
                IController controller = resolvedController as IController;
     
                return controller;
            }
        }
     
        public class InterfaceMappings : NinjectModule
        {
            /// <summary>
            /// Loads the module into the kernel.
            /// </summary>
            public override void Load()
            {
                Bind<INewsItemModelsService>().To<NewsItemModelsService>();
            }
        }
    }

    Factory init, notice it is on application bootstrapped:
    Copy Code
    protected void Application_Start(object sender, EventArgs e)
           {
               Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;      
           }
     
          protected  void Bootstrapper_Bootstrapped(object sender, EventArgs e)
           {
               ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(new ContainerControlledLifetimeManager());
               var factory = ObjectFactory.Resolve<ISitefinityControllerFactory>();
               ControllerBuilder.Current.SetControllerFactory(factory);
           }

    You can put the Inject attribute to the constructor you want to inject with parameters. Otherwise, the order is the following:
    1. Constructors marked with [Inject]
    2. Construtors with the most parameter
    3. Default contructor


    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Narasimha
    Narasimha avatar
    14 posts
    Registered:
    26 May 2015
    04 Jul 2015 in reply to Nikola Zagorchev
    Link to this post

    Thanks for the details, I tried this today and it does not work for me. I compiled the code and I end up getting an error on compile which is as follows

     

    Error33Unable to load assembly 'Cola.UI, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
    ExceptionString:
    System.SystemException: Unable to load assembly 'Cola.UI, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
       at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
       at System.Reflection.RuntimeModule.GetTypes()
       at System.Reflection.Assembly.GetTypes()
       at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
       --- End of inner exception stack trace ---
       at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
       at Telerik.OpenAccess.Metadata.MetadataAttributesReader.ReadMetaModel(Assembly assembly)
       at Telerik.OpenAccess.Metadata.AttributesMetadataSource.<>c__DisplayClass1.<FromAssembly>b__0()
       at Telerik.OpenAccess.Metadata.AttributesMetadataSource.CreateModel()
       at Telerik.OpenAccess.Metadata.MetadataSource.GetModelCore(MetadataContainer old)
       at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
       at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()C:\TFSLocal\COLA\UI\Cola.UI\obj\Debug\Cola.UI.dllCola.UI

  4. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    06 Jul 2015
    Link to this post
    Hi Narasimha,

    I doubt the error you are receiving is connected with the Ninject sample. It is an assembly references error. Please, check the referenced Telerik.Sitefinity version, as well as, from where it is read - the project bin folder or from nuget packages.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  5. Vladimir
    Vladimir avatar
    13 posts
    Registered:
    30 Apr 2015
    06 Nov 2015
    Link to this post

    I have the same problem on my project, after changing the Telerik references from /bin folder to Nuget packages.

    The oddest thing is that if I comment the NinjectControllerFactory that inherits from FrontendController Factory it builds successfully the solution.

    The error seams to be thrown by OpenAccess enhacer.exe

    I'm using Sitefinity Version=8.0.5710.0.  Have no idea why it needs Version=8.0.5700.0. There is no reference to it throughout the project, neither in web.config

    This is my error:

    [PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: Unable to load assembly 'SitefinityWebApp, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
    [PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: ExceptionString:
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0: System.SystemException: Unable to load assembly 'SitefinityWebApp, Version=8.0.5710.0, Culture=neutral, PublicKeyToken=null' : Could not load file or assembly 'Telerik.Sitefinity, Version=8.0.5700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500) ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
    4>C:\Development\TerminalManagement\mytemporaryalphawebsite\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at System.Reflection.RuntimeModule.GetTypes()
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at System.Reflection.Assembly.GetTypes()
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    --- End of inner exception stack trace ---
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.MetadataAttributesReader.PrepareMetaModelTypes(Assembly assembly)
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.MetadataAttributesReader.ReadMetaModel(Assembly assembly)
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.AttributesMetadataSource.<>c__DisplayClass1.<FromAssembly>b__0()
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.AttributesMetadataSource.CreateModel()
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Metadata.MetadataSource.GetModelCore(MetadataContainer old)
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
    4>[PATH_TO_SOLUTION_FOLDER]\obj\Debug\SitefinityWebApp.dll(-1,-1): OpenAccess Enhancer error 0:    at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()

     

    Could you please help me? Many thanks!!

  6. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Nov 2015
    Link to this post
    Hello Vladimir,

    Please, try resolving the issue using a binding redirect:
    http://www.sitefinity.com/developer-network/knowledge-base/details/could-not-load-file-or-assembly-error-when-upgrading-to-sitefinity

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  7. Kyrylo
    Kyrylo avatar
    1 posts
    Registered:
    25 Mar 2016
    11 Apr
    Link to this post
    Actually, sitefinity never calls GetControllerInstance method. I still did not find how to make any container to work correctly  within sitefinity. 
  8. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    13 Apr
    Link to this post
    Hello Kyrylo,

    You still have to add the ninject module registration:
            protected void Application_Start(object sender, EventArgs e)
            {
                Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
            }
     
            protected void Bootstrapper_Bootstrapped(object sender, EventArgs e)
            {
                // DI binding
                ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(new ContainerControlledLifetimeManager());
                var factory = ObjectFactory.Resolve<ISitefinityControllerFactory>();
                ControllerBuilder.Current.SetControllerFactory(factory);
            }
     
    ...
    public class NinjectControllerFactory: FrontendControllerFactory
        {
             private IKernel ninjectKernel;
     
             public NinjectControllerFactory()
             {
                 ninjectKernel = Telerik.Sitefinity.Frontend.FrontendModule.Current.DependencyResolver;
                ninjectKernel.Bind<IAuthorService>().To<AuthorService>();
             }
     
             protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
             {
                 if (controllerType == null)
                 {
                     return null;
                 }
     
                 var resolvedController = this.ninjectKernel.Get(controllerType);
                 IController controller = resolvedController as IController;
     
                 return controller;
             }   
        }

    Hope this helps.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
    Answered
  9. Jayson
    Jayson avatar
    3 posts
    Registered:
    23 Oct 2015
    14 Apr in reply to Nikola Zagorchev
    Link to this post

    Hi Thank you for the code.

    I was able to make this work by switching the registration from

                Bootstrapper.Initialized += (obj, ev) =>             {                 if (ev.CommandName != "Bootstrapped") return;
              -- do the registration 
    to

     

    Bootstrapper.Bootstrapped += (obj, ev) =>  {
      -- do the registration
    }

    Thanks a lot.

    Cheers.
    J



  10. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    14 Apr
    Link to this post
    Hi Jayson,

    I am glad you have managed to make it work.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  11. Balanjaneyulu
    Balanjaneyulu avatar
    35 posts
    Registered:
    11 Feb 2016
    12 Jul
    Link to this post


    Hi everyone,

    I was developing one custom widget and had the same issue so I have followed this forum but still I am facing the same issue.

    Can someone help me how to solve this issue?

    FYI.

    https://drive.google.com/open?id=0B1Z7d8OTSWR4OUw5TXlmS014Umc

    Please find the file here.

    Thanks,
    Balu
  12. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Jul
    Link to this post
    Hi Balu,

    What is the exception and stack trace that you get? The code in the Global application class seems correct.

    Regards,
    Nikola Zagorchev
    Telerik by Progress
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  13. Balanjaneyulu
    Balanjaneyulu avatar
    35 posts
    Registered:
    11 Feb 2016
    12 Jul
    Link to this post
    Hi Nikola,

    I was getting below exception message.


    An exception of type 'Ninject.ActivationException' occurred in Ninject.dll but was not handled in user code

    Additional information: Error activating ICorticonFormModel

    No matching bindings are available, and the type is not self-bindable.

    Activation path:

      1) Request for ICorticonFormModel



    Suggestions:

      1) Ensure that you have defined a binding for ICorticonFormModel.

      2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.

      3) Ensure you have not accidentally created more than one kernel.

      4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.

      5) If you are using automatic module loading, ensure the search path and filters are correct.

    Thanks,
    Balu
  14. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Jul
    Link to this post
    Hello,

    You do not have the Bootstrapped event attached to:
    protected void Application_Start(object sender, EventArgs e)
            {
                Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
            }


    Regards,
    Nikola Zagorchev
    Telerik by Progress
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  15. Balanjaneyulu
    Balanjaneyulu avatar
    35 posts
    Registered:
    11 Feb 2016
    12 Jul
    Link to this post


    Hi Nikola,

    Thank you. I have attached the Bootstrap event in my  Global.ascx.cs file.


    protected void Application_Start(object sender, EventArgs e)
            {
                Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
            }

    Thanks,
    Balu
  16. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    12 Jul
    Link to this post
    Hello Balu,

    In your case, you need to attach to both events, since you execute code in both:
       Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += Bootstrapper_Initialized;
    and
    Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;

    In the current implementation the Bootstrapper_Bootstrapped handler where the Ninject factory is registered is not executed at all.

    Regards,
    Nikola Zagorchev
    Telerik by Progress
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  17. Balanjaneyulu
    Balanjaneyulu avatar
    35 posts
    Registered:
    11 Feb 2016
    12 Jul
    Link to this post
    Hi Nikola,

    Thank you so much. My issue got resolved and widget is working as expected.

    Thanks,
    Balu
  18. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    13 Jul
    Link to this post
    Hi Balu,

    I am glad the issue is resolved.

    Regards,
    Nikola Zagorchev
    Telerik by Progress
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
18 posts, 1 answered