-
Notifications
You must be signed in to change notification settings - Fork 10
Description
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.
- Checkout the https://github.com/nathanrobb/AspNetDependencyInjectionWebServiceExample repo
- Run the project (optional: clear the debug output once loaded)
- 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]]
- Navigate to
http://localhost:56145/Test.asmx?op=HelloWorld(WebService)- Click Invoke, inspect the debug output for object hash codes
- 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]]
- Navigate to
http://localhost:56145/api/test(WebAPI)- e.g. Throws exception
HttpContextBase has not been set for this Service Scope - This is happening even though we have already created a
TestObjectfor the request inGlobal.asmx.cswhy are we making another one? Why is theIHttpContextAccessornot configured correctly? We have the same thread localHttpContextthroughout.
- e.g. Throws exception
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.