Skip to content

WebForms InRequestScope not working #45

@BloodBaz

Description

@BloodBaz

Hi,
I've raised this on Stack Overflow a couple of weeks ago but its not really drawing attention of anyone who knows what this issue is occurring. However, I do believe that InRequestScope isn't working for pages that implement IHttpHandler, [Inject] attributed properties or service locator injections.

The attached sample demonstrates a clean single page classic ASP.NET web application which uses Ninject.Web.Common.WebHost to bind a number of services using InRequestScope (see App_Start\NinjectWebCommon.cs) .

When the test page (TestPage.aspx) is requested and the root service (IServiceA) is requested, the downstream services are instantiated multiple times (see output log).

The same issue occurs for the test handler (TestHandler.ashx).

Furthermore, the [Inject] attribute on the property is not being set in the test page (TestPage.aspx).

NOTE: RegisterServices() is as follows:

	private static void RegisterServices(IKernel kernel)
	{
		kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
		kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

		kernel.Bind<IServiceA>().To<Services>().InRequestScope();
		kernel.Bind<IServiceB>().To<ServiceB>().InRequestScope();
		kernel.Bind<IServiceC>().To<ServiceC>().InRequestScope();
		kernel.Bind<IServiceD>().To<ServiceD>().InRequestScope();
		kernel.Bind<IServiceE>().To<ServiceE>().InRequestScope();
		kernel.Bind<IServiceF>().To<ServiceF>().InRequestScope();
	}

Requests for IServiceA reports this in the output log:

Constructing Service F
Constructing Service D
Constructing Service F
Constructing Service E
Constructing Service B
Constructing Service F
Constructing Service D
Constructing Service F
Constructing Service E
Constructing Service C
Constructing Service A

Expected output:

Constructing Service F
Constructing Service D
Constructing Service E
Constructing Service B
Constructing Service C
Constructing Service A

TestInRequestScopeWeb.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions