diff --git a/src/Nito.AsyncEx.Coordination/AsyncLazy.cs b/src/Nito.AsyncEx.Coordination/AsyncLazy.cs index db9811b..a69fd93 100644 --- a/src/Nito.AsyncEx.Coordination/AsyncLazy.cs +++ b/src/Nito.AsyncEx.Coordination/AsyncLazy.cs @@ -40,11 +40,6 @@ public sealed class AsyncLazy /// private readonly object _mutex; - /// - /// The factory method to call. - /// - private readonly Func> _factory; - /// /// The underlying lazy task. /// @@ -77,14 +72,13 @@ public AsyncLazy(Func> factory, AsyncLazyFlags flags = AsyncLazyFlags.No { if (factory == null) throw new ArgumentNullException(nameof(factory)); - _factory = factory; - if ((flags & AsyncLazyFlags.RetryOnFailure) == AsyncLazyFlags.RetryOnFailure) - _factory = RetryOnFailure(_factory); if ((flags & AsyncLazyFlags.ExecuteOnCallingThread) != AsyncLazyFlags.ExecuteOnCallingThread) - _factory = RunOnThreadPool(_factory); + factory = RunOnThreadPool(factory); + if ((flags & AsyncLazyFlags.RetryOnFailure) == AsyncLazyFlags.RetryOnFailure) + factory = RetryOnFailure(factory); _mutex = new object(); - _instance = new Lazy>(_factory); + _instance = new Lazy>(factory); } /// @@ -121,17 +115,18 @@ public Task Task private Func> RetryOnFailure(Func> factory) { - return async () => + var originalFactory = factory; + return factory = async () => { try { - return await factory().ConfigureAwait(false); + return await originalFactory().ConfigureAwait(false); } catch { lock (_mutex) { - _instance = new Lazy>(_factory); + _instance = new Lazy>(factory); } throw; }