Open mconnew opened 1 year ago
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
Background and Motivation
There are two steps to shutting down a
WebApplication
, callingStopAsync
, and disposing it. Omitting either of these steps can have negative consequences. If someone is transitioning fromWebHost
toWebApplication
, the dispose behavior is different and can lead to introducing a bug/unwanted behavior. If you don't callWebHost.StopAsync
, when you dispose theWebHost
it implicitly callsStopAsync
.WebApplication
does not ensureStopAsync
has been called when you dispose it. The consequences of this is that anyIHostedService
instances won't have theirStopAsync
method called when disposing aWebApplication
whereas when usingWebHost
they did.The second problem is if you omit disposing your
WebApplication
, you will leak some memory which is never reclaimed by GC. See #45621 for details.Proposed Analyzer
Analyzer Behavior and Message
If a
WebApplication
is used with a using statement and there is not a call to StopAsync within the using statement code block, then an info message should be emitted informing the developer that anyIHostedService
instances may not be cleanly stopped if disposing without calling StopAsync. Similarly if not using a using block but manually callingDispose
/DisposeAsync
, this is also the case. This would be an info usage rule.If a call to
WebApplication.StopAsync
is made without a subsequent call toWebApplication.Dispose()
orWebApplication.DisposeAsync()
, then a warning should be emitted telling the developer that they will leak memory. Alternatively, fix issue #45621 and then this won't be needed, but there doesn't seem to be any traction on that issue. This would be a warning reliability rule.Category
Severity Level
Usage Scenarios
This should trigger the info about needing to call
StopAsync
:This code should also trigger the info about needing to call
StopAsync
:Followed by one of the following lines:
This should trigger the warning about needing to dispose the WebApplication
Risks
The overhead of running the analyzer will take a non-zero amount of time.