Problem Integrating Ninject v2 with Asp.net Webforms
I am trying to integrate Ninject v2 with my asp.net webforms project. I am running .NET Framework 4.0.
My global.asax inherits from Ninject.Web.NinjectHttpApplication. I have 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 have also added the following httpmodule entry to my web.config:
<add name="NinjectHttpModule" type="Ninject.Web.NinjectHttpModule, Ninject.Web">
However when i run the application an InvalidOperationException is fired with the following:
"The type ASP.login_aspx requested an injection, but no kernel has been registered for the web application. Please ensure that your project defines a NinjectHttpApplication."
What am i doing wrong?
开发者_开发技巧Kind Regards
Lee
I had the same error in my ASP.NET 4.0 Webforms application. I fixed that by using sole 'Global.asax' file with code, instead of 'Global.asax' + 'Global.asax.cs' code behind. E.g. you can use this standalone 'Global.asax' file:
<%@ 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>
Also I never added any httpmodules to web.config, and it worked just fine!
When you create a new web application, the Global.asax.cs file is filled with empty methods:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
The NinjectHttpApplication also implements these methods, but the empty methods in the generated Global.asax.cs never call the methods on NinjectHttpApplication. To solve this, remove the empty methods or add a call to the base method:
void Application_Start(object sender, EventArgs e)
{
base.Application_Start();
}
My case is a bit different - thought I'd share w/ you guys here - was testing it under IIS 7 then deployed it on Production Server which was IIS 6.
Got same error message '.. but no kernel has been registered for the web application... Please ensure that your project defines a NinjectHttpApplication.'
Tried all the above-mentioned solutions - none worked. Found out that events in Global.asax.cs were somehow ignored.
To solve this, I got the file PrecompiledApp.config
removed - may also need Recycling.
Cheers.
I believe you also need to register the HttpApplicationInitializationModule
<add name="HttpApplicationInitializationModule" type="Ninject.Web.Common.HttpApplicationInitializationModule, Ninject.Web.Common"/>
精彩评论