Closed Jasdo closed 4 years ago
Do you have the full stacktrace for the exception?
Is this on IIS or Kestrel? Seems like IIS.
If you observe and categorize the failures as shown by HttpClient, what's the breakdown that timeout, receive 500s, etc.? This may be related to the client timing out and disconnecting. Are there any more interesting failures reported by the client?
Also, how does it behave if you switch to HTTP/2?
services.AddSingleton<WeatherForecastController>();
Does it work if you remove this? I'm not sure Controllers are supposed to be singletons. (cc @rynowak)
services.AddSingleton<WeatherForecastController>();
Does it work if you remove this? I'm not sure Controllers are supposed to be singletons. (cc @rynowak)
Yes, it works. The problem is that it creates a new instance of the controller for each REST call, and I'm trying to avoid that. Of course I can move the part that I want to be a "singleton" to another class, but everything worked fine before we migrated to 3.1 from 2.1. I assume it is related to this change in FeatureReferences.
The problem is that it creates a new instance of the controller for each REST call, and I'm trying to avoid that.
Why are you trying to avoid that? If you need a singleton, it should be a service consumed by the controller, not the controller itself.
I checked with MVC and controllers are not supported as singletons. They have request state, so even if you didn't hit issues like this, you'd still have concurrency problems.
The problem is that it creates a new instance of the controller for each REST call, and I'm trying to avoid that.
Why are you trying to avoid that? If you need a singleton, it should be a service consumed by the controller, not the controller itself.
Yes, this is how it is implemented now in our code, the thing is that using the controller as a singleton worked until we migrated from ASP .NET Core 2.1 to 3.1.
@Tratcher, @anurse Thank you for your help!
Steps to reprodce
The result is that eventually, a System.ObjectDisposedException will be thrown upon accessing HttpContext.Request.Headers.
Startup code
Controller code
Client code
Further technical details
Exception details: System.ObjectDisposedException HResult=0x80131622 Message=IFeatureCollection has been disposed. Object name: 'Collection'. Source=Microsoft.AspNetCore.Http.Features StackTrace: at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed() in /_/src/Http/Http.Features/src/FeatureReferences.cs:line 120
ASP.NET Core version - 3.1.0
Output of
dotnet --info
: dotnetInfo.txtThe IDE is VS2019 Version 16.4.1