WebForms中的Ninject 3

时间:2012-11-27 15:44:20

标签: ninject ninject.web

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

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

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

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

我基本上遵循了包含How can I implement Ninject or DI on asp.net Web Forms?

的设置建议

痛苦的黄色屏幕说

[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

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

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

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