Ninject内核突然失去所有绑定然后再次开始工作

时间:2016-08-18 08:59:55

标签: c# asp.net-mvc-5 ninject

我使用Ninject作为依赖注入工具。我有相当大的asp.net-mvc 5项目(80个绑定),当我单步执行我的代码时,在内核中完美地创建了绑定.NinjectWebCommon.cs。文件偶尔会在以下代码行中失败

(kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);) 

导致创建新内核,并且我丢失了对SQL Server数据库的所有绑定。

   private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            RegisterServices(kernel);
            AddBindings(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

结果是当我执行代码时,我在视图中遇到NULL模型错误。错误已经自动纠正,但我需要知道是什么导致它,以便我可以宁愿处理它。我包含完整的NinjectWebCommon.cs文件

using eMedic.Domain.Abstract;
using eMedic.Domain.Concrete;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(eMedic.WebUI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(eMedic.WebUI.App_Start.NinjectWebCommon), "Stop")]

namespace eMedic.WebUI.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                RegisterServices(kernel);
                AddBindings(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        private static void AddBindings(IKernel kernel)
        {
            // add bindings here
            kernel.Bind<IAccountRepository>().To<EFAccountRepository>();
            kernel.Bind<IAddressRepository>().To<EFAddressRepository>();
            kernel.Bind<IAddressTypeRepository>().To<EFAddressTypeRepository>();

          //other bindings as well (omitted for brevity)
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            System.Web.Mvc.DependencyResolver.SetResolver(new eMedic.WebUI.Infrastructure.NinjectDependencyResolver(kernel));
        }        
    }
}

以及NinjectDependencyResolver.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using eMedic.Domain.Abstract;
using eMedic.Domain.Concrete;

namespace eMedic.WebUI.Infrastructure
{
    public class NinjectDependencyResolver : IDependencyResolver
    {
        private IKernel kernel;
        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;

        }
        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }


    }
}

数据接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eMedic.Domain.Abstract
{
    public interface IAddressRepository
    {
        IEnumerable<address> Address { get; }

    }
}

和其他数据接口......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using eMedic.Domain.Abstract;

namespace eMedic.Domain.Concrete
{
   public class EFAddressRepository:IAddressRepository
    {
        private EFDbContext context = new EFDbContext();
        public IEnumerable<address> Address
        {
            get { return context.address; }
        }




    }
}

我真的很感激帮助,以便找到错误的根本原因。

1 个答案:

答案 0 :(得分:0)

我设法弄清楚问题是什么,仅供参考我在这里添加原因: 我在SQL Server数据库中引用的其中一个表中的外键(FK)值出错。它有一个 NULL 值,当我更改记录以在该特定表中为该特定FK赋值时,错误被清除。