Ninject MVC with Sitefinity - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Ninject MVC with Sitefinity

  • Ninject MVC with Sitefinity
  • 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.​

  • 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
     
  • 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

  • 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
     
  • 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!!

  • 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
     
  • Actually, sitefinity never calls GetControllerInstance method. I still did not find how to make any container to work correctly  within sitefinity. 
  • 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
     
  • 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
     
  • 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





  • 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.

    drive.google.com/open

    Please find the file here.

    Thanks,
    Balu
  • 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
     
  • 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
  • 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