Ninject + “Error loading Ninject component ICache”

后端 未结 3 1180
故里飘歌
故里飘歌 2021-01-19 06:52

I\'ve just installed the new Ninject.MVC3 from NuGet and trying to make it work in my asp.net mvc 3 app, however I get this weird error now and then when surfing my site:

相关标签:
3条回答
  • 2021-01-19 07:05

    Looks like its a bug according to this thread, and that they are working on a fix...

    0 讨论(0)
  • 2021-01-19 07:07

    Fixed in 2.2.1.0

    See http://www.planetgeek.ch/2011/03/01/ninject-2-2-1-0-and-ninject-mvc3-2-2-1-0-released/ for more information.

    0 讨论(0)
  • 2021-01-19 07:16

    Like previously mentioned it does look like a bug.
    One option is to simply implement a singleton extension method yourself:

    public static class NinjectSingletonExtension
    {
        public static CustomSingletonKernelModel<T> SingletonBind<T>(this IKernel i_KernelInstance)
        {
            return new CustomSingletonKernelModel<T>(i_KernelInstance);
        }
    }
    
    public class CustomSingletonKernelModel<T>
    {
        private const string k_ConstantInjectionName = "Implementation";
        private readonly IKernel _kernel;
        private static object padlock = new Object();
    
        private T _concreteInstance;
    
    
        public CustomSingletonKernelModel(IKernel i_KernelInstance)
        {
            this._kernel = i_KernelInstance;
        }
    
        public IBindingInNamedWithOrOnSyntax<T> To<TImplement>(TImplement i_Constant = null) where TImplement : class, T
        {
            _kernel.Bind<T>().To<TImplement>().Named(k_ConstantInjectionName);
            var toReturn =
                _kernel.Bind<T>().ToMethod(x =>
                                           {
                                               if (i_Constant != null)
                                               {
                                                   return i_Constant;
                                               }
    
                                               if (_concreteInstance == null)
                                               {
                                                   lock (padlock)
                                                   {
                                                       if (_concreteInstance == null)
                                                       {
                                                           _concreteInstance = _kernel.Get<T>(k_ConstantInjectionName);
                                                       }
                                                   }
                                               }
    
    
                                               return _concreteInstance;
                                           }).When(x => true);
    
            return toReturn;
        }
    }
    

    And then simply use:

    i_Kernel.SingletonBind<T>().To<TImplement>();
    

    Rather then

    i_Kernel.Bind<T>().To<TImplement>().InSingletonScope();
    
    0 讨论(0)
提交回复
热议问题