When one wishes to dispose of IDisposable
objects with OWIN, it was been widely recommended to use the CancellationToken
from the host, should it be available, like so:
var context = new OwinContext(builder.Properties);
var token = context.Get<CancellationToken>("host.OnAppDisposing");
if (token != CancellationToken.None)
{
token.Register(() =>
{
disposable.Dispose();
});
}
I've registered some 12 objects for disposal with the above code inside of a foreach
and I've found that some number of these will be called throughout the day leaving me with some resources MIA while the others remain as if nothing had happened.
Unfortunately, the documentation for both host.OnAppDisposing
and the CancellationToken
itself has offered me little information. There is no mention of lifecycle on the former and the latter has no mention regarding limits to Register
.
Is there a consistent and well-documented way to handle resource disposal in a long-running ASP.NET application running on IIS?
question from:
https://stackoverflow.com/questions/65910933/host-onappdisposing-intermittently-invoked 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…