Skip to content

WebApi Request Scope bug #29

@nathanrobb

Description

@nathanrobb

Hi Team,

We've been using this library for a while in ASP.NET Web Forms, and it's been working great!

Recently we added WebApi endpoints into our Web Forms site as they are much nicer to work with than WebServices (See #24) and discovered some strange request scope behaviour. This likely also relates to #25.

It appears that the ASP.NET request scope != WebApi request scope, and also the WebApi request scope doesn't have the DefaultHttpContextAccessor.HttpContext configured (while the ASP.NET request scope does), this causes the The HttpContextBase has not been set for this Service Scope. exception to fire when using IHttpContextAccessor in a WebAPI controller.

I have extended my minimal reproducing project here: https://github.com/nathanrobb/AspNetDependencyInjectionWebServiceExample to include our WebAPI issue outlined below.

This is using:
Jehoel.AspNetDependencyInjection @ 5.0.0-beta2
Jehoel.AspNetDependencyInjection.WebApi @ 5.0.0-beta2

To reproduce:
I am logging some hash codes to the debugger window. I would expect these hash codes to be equal for all operations within a request.

  1. Checkout the https://github.com/nathanrobb/AspNetDependencyInjectionWebServiceExample repo
  2. Run the project (optional: clear the debug output once loaded)
  3. Navigate to http://localhost:56145/TestPage.aspx (ASP page)
Making TestObject [Context (Thread) Hashcode: 19612312]
Global [2021-11-11T20:44:52.0391706Z - [Instance: F1C118AD-6C71-4558-A480-9B441BC79D76] [Context (DI) HashCode: 43226536] [Context (Thread) HashCode: 19612312]]
ASP Page [2021-11-11T20:44:52.0401678Z - [Instance: F1C118AD-6C71-4558-A480-9B441BC79D76] [Context (DI) HashCode: 43226536] [Context (Thread) HashCode: 19612312]]
  1. Navigate to http://localhost:56145/Test.asmx?op=HelloWorld (WebService)
    1. Click Invoke, inspect the debug output for object hash codes
    2. e.g (these all the same object throughout the request)
Making TestObject [Context (Thread) Hashcode: 8462621]
Global [2021-11-11T20:46:37.9495927Z - [Instance: AADA59AA-9DC1-4BD6-8B36-4F36CF68C4D7] [Context (DI) HashCode: 44681534] [Context (Thread) HashCode: 8462621]]
WebService [2021-11-11T20:46:37.9655496Z - [Instance: AADA59AA-9DC1-4BD6-8B36-4F36CF68C4D7] [Context (DI) HashCode: 44681534] [Context (Thread) HashCode: 8462621]]
  1. Navigate to http://localhost:56145/api/test (WebAPI)
    1. e.g. Throws exception HttpContextBase has not been set for this Service Scope
    2. This is happening even though we have already created a TestObject for the request in Global.asmx.cs why are we making another one? Why is the IHttpContextAccessor not configured correctly? We have the same thread local HttpContext throughout.
Making TestObject [Context (Thread) Hashcode: 37460558]
Global [2021-11-11T20:47:17.8691440Z - [Instance: 6B37B561-49E6-432D-8DBB-D49364EEF978] [Context (DI) HashCode: 59487907] [Context (Thread) HashCode: 37460558]]
Making TestObject [Context (Thread) Hashcode: 37460558]
Exception thrown: 'System.InvalidOperationException' in AspNetDependencyInjection.dll
An exception of type 'System.InvalidOperationException' occurred in AspNetDependencyInjection.dll but was not handled in user code
The HttpContextBase has not been set for this Service Scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions