Ninject + “Error loading Ninject component ICache”

后端 未结 3 1179
故里飘歌
故里飘歌 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: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 SingletonBind(this IKernel i_KernelInstance)
        {
            return new CustomSingletonKernelModel(i_KernelInstance);
        }
    }
    
    public class CustomSingletonKernelModel
    {
        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 To(TImplement i_Constant = null) where TImplement : class, T
        {
            _kernel.Bind().To().Named(k_ConstantInjectionName);
            var toReturn =
                _kernel.Bind().ToMethod(x =>
                                           {
                                               if (i_Constant != null)
                                               {
                                                   return i_Constant;
                                               }
    
                                               if (_concreteInstance == null)
                                               {
                                                   lock (padlock)
                                                   {
                                                       if (_concreteInstance == null)
                                                       {
                                                           _concreteInstance = _kernel.Get(k_ConstantInjectionName);
                                                       }
                                                   }
                                               }
    
    
                                               return _concreteInstance;
                                           }).When(x => true);
    
            return toReturn;
        }
    }
    

    And then simply use:

    i_Kernel.SingletonBind().To();
    

    Rather then

    i_Kernel.Bind().To().InSingletonScope();
    

提交回复
热议问题