4

当我尝试启动安装了 Ninject.Web (v3) 的 WebForms 应用程序时,出现以下错误。

“静态容器已经有一个与之关联的内核!”

我在 Global.asax 中实现 Ninject,继承自 NinjectHttpApplication 并覆盖 CreateKernel(),我的模块采用以下形式

public class NinjectWebModule: NinjectModule
    {
        public override void Load()
        {
            Bind<IBlah>().To<Blah>();
        }
    }

我基本上遵循了包含如何在 asp.net Web 表单上实现 Ninject 或 DI 的设置建议?

疼痛的黄屏说

[NotSupportedException: The static container already has a kernel associated with it!]
   Ninject.Web.KernelContainer.set_Kernel(IKernel value) in c:\Projects\Ninject\ninject.web\src\Ninject.Web\KernelContainer.cs:38
   Ninject.Web.NinjectWebHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web\src\Ninject.Web\NinjectWebHttpApplicationPlugin.cs:62
   Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
   Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:32
   Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
   Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:80

[HttpException (0x80004005): The static container already has a kernel associated with it!]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9859725
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): The static container already has a kernel associated with it!]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9873912
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

有任何想法吗?

4

1 回答 1

6

嗬!安装 Ninject.Web (v3) 会添加一个带有 NinjectWebCommon.cs 的 App_Start 文件夹

这是您注册绑定的地方,而不是 Global.asax

于 2012-11-27T15:59:49.513 回答