AsyncLazy will cache the exceptions

Author: gravity00Created Oct 23, 2018Updated May 30, 2024

https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/5be73c43adf7897203b789854a43867d478b76f4/Scenarios/Services/RemoteConnection.cs#L55-L90

I believe it should be at least noted that if an exception occurs inside the factory function, the exception will be cached by the Task and since everything is a singleton, the application won't be able to recover from the failed state without some restart (unless I'm missing something).

Obviously this may or may not be a problem, depends on the developer needs, but usually this isn't the intended behavior. Most commonly is like not adding an item to a cache if the initialization fails.

Source: davidfowl/AspNetCoreDiagnosticScenarios