How to use Lazy(Func<T> valueFactory) of C# in such a way that, if initialization fails, it should not use cached Exception and at the same time it should not run multiple threads for initialization?
If I use LazyThreadSafetyMode.PublicationOnly as a second parameter, then it will run multiple threads for initialization on start and throws exceptions from all threads if exception occurs in initialization. But if I use LazyThreadSafetyMode.ExecutionAndPublication as a second parameter it will cache the exception and rethrow the same and never reinitialize it again.
I need a pattern/version where it should run on single thread for initialization and should not cache exceptions.
I can't use constructor version as I have to conditionally create different instances of T on the basis of some parameters set at runtime,
I am using Lazy<TInterface>(func()) and conditionally returning different implementations.