Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebApi Request Scope bug #29

Open
nathanrobb opened this issue Nov 11, 2021 · 0 comments
Open

WebApi Request Scope bug #29

nathanrobb opened this issue Nov 11, 2021 · 0 comments

Comments

@nathanrobb
Copy link

nathanrobb commented Nov 11, 2021

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.
nathanrobb added a commit to nathanrobb/AspNetDependencyInjectionWebServiceExample that referenced this issue Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant