Problem Integrating Ninject v2 with Asp.net Web Formats

I am trying to integrate Ninject v2 with my asp.net webforms project. I am running the .NET Framework 4.0.

My global.asax inherits from Ninject.Web.NinjectHttpApplication. I also added the CreateKernel method:

protected override IKernel CreateKernel()
{
  IKernel kernel = new StandardKernel();
  kernel.Bind<IUser>().To<User>();
  return kernel;
}

All my pages inherit from Ninject.Web.PageBase. I also added the following httpmodule entry to my web.config:

<add name="NinjectHttpModule" type="Ninject.Web.NinjectHttpModule, Ninject.Web">

However, when I launch the application, an InvalidOperationException fires with the following:

"Type ASP.login_aspx requested an injection, but the kernel was not registered for the web application. Make sure your project defines NinjectHttpApplication.

What am I doing wrong?

Yours faithfully

Lee

+5
source share
4 answers

ASP.NET 4.0 Webforms. , "Global.asax" , "Global.asax" + "Global.asax.cs". . "Global.asax" :

<%@ Application Language="C#" Inherits="Ninject.Web.NinjectHttpApplication" %>
<%@ Import Namespace="Ninject" %>

<script runat="server">
    protected override IKernel CreateKernel() {
        var kernel = new StandardKernel();
        kernel.Bind<IUser>().To<User>();
        return kernel;
    }
</script>

httpmodules web.config, !

+3

- Global.asax.cs :

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
    }

NinjectHttpApplication , Global.asax.cs NinjectHttpApplication. , :

    void Application_Start(object sender, EventArgs e)
    {
        base.Application_Start();
    }
+3

- , , , IIS 7, Production Server, IIS 6.

'.., ​​ -... , , NinjectHttpApplication.'

- . , Global.asax.cs - .

, PrecompiledApp.config, .

.

+2

, HttpApplicationInitializationModule

<add name="HttpApplicationInitializationModule" type="Ninject.Web.Common.HttpApplicationInitializationModule, Ninject.Web.Common"/>
0

All Articles