public CloseAsyncResult(SingletonDictionaryManager <TKey, TInstance> owner, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state) { List <TKey> tKeys; this.owner = owner; this.syncRoot = new object(); this.timeoutHelper = new Microsoft.ServiceBus.Common.TimeoutHelper(timeout); lock (this.owner.ThisLock) { tKeys = new List <TKey>(this.owner.instances.Keys); } this.IncreaseWaitCount(); foreach (TKey tKey in tKeys) { try { this.IncreaseWaitCount(); (new SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult(this.owner, tKey, null, false, this.timeoutHelper.RemainingTime(), SingletonDictionaryManager <TKey, TInstance> .CloseAsyncResult.OnUnloadCompletedCallback, this)).Start(); } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } this.DecreaseWaitCount(exception, true); } } this.DecreaseWaitCount(null, true); }
protected TInstance GetInstance(TKey key, TimeSpan timeout) { SingletonDictionaryManager <TKey, TInstance> .GetInstanceAsyncResult getInstanceAsyncResult = new SingletonDictionaryManager <TKey, TInstance> .GetInstanceAsyncResult(this, key, timeout, null, null); getInstanceAsyncResult.RunSynchronously(); return(getInstanceAsyncResult.Instance); }
protected TInstance LoadInstance(TKey key, object loadingContext, TimeSpan timeout) { SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult loadInstanceAsyncResult = new SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult(this, key, loadingContext, timeout, null, null); loadInstanceAsyncResult.RunSynchronously(); return(loadInstanceAsyncResult.Instance); }
private void IncreaseWaitCount() { lock (this.syncRoot) { SingletonDictionaryManager <TKey, TInstance> .CloseAsyncResult closeAsyncResult = this; closeAsyncResult.waitCount = closeAsyncResult.waitCount + 1; } }
public LoadInstanceAsyncResult(SingletonDictionaryManager <TKey, TInstance> owner, TKey key, object loadingContext, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state) { this.owner = owner; this.key = key; this.loadingContext = loadingContext; SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult loadInstanceAsyncResult = this; loadInstanceAsyncResult.OnCompleting = (Action <AsyncResult, Exception>)Delegate.Combine(loadInstanceAsyncResult.OnCompleting, SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult.CompletingDelegate); }
public UnloadInstanceAsyncResult(SingletonDictionaryManager <TKey, TInstance> owner, TKey key, object unloadingContext, bool shouldAbort, TimeSpan timeout, AsyncCallback callback, object state) : base(TimeSpan.MaxValue, callback, state) { this.owner = owner; this.key = key; this.unloadingContext = unloadingContext; this.shouldAbort = shouldAbort; this.closeTimeout = timeout; SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult = this; unloadInstanceAsyncResult.OnCompleting = (Action <AsyncResult, Exception>)Delegate.Combine(unloadInstanceAsyncResult.OnCompleting, SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult.CompletingDelegate); }
protected bool TryGetSingletonContext(TKey key, out SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext) { bool flag; lock (base.ThisLock) { flag = this.instances.TryGetValue(key, out singletonContext); } return(flag); }
protected override void OnAbort() { lock (base.ThisLock) { foreach (TKey list in this.instances.Keys.ToList <TKey>()) { SingletonDictionaryManager <TKey, TInstance> .SingletonContext item = this.instances[list]; if (item.State != SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loaded) { continue; } this.instances.Remove(list); this.OnAbortInstance(item, list, item.Instance, null); } } }
private static void OnUnloadCompleted(IAsyncResult result) { SingletonDictionaryManager <TKey, TInstance> .CloseAsyncResult asyncState = (SingletonDictionaryManager <TKey, TInstance> .CloseAsyncResult)result.AsyncState; try { SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult.End(result); asyncState.DecreaseWaitCount(null, result.CompletedSynchronously); } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } asyncState.DecreaseWaitCount(exception, result.CompletedSynchronously); } }
private void DecreaseWaitCount(Exception exception, bool isCompletedSynchronously) { bool flag = false; lock (this.syncRoot) { if (this.firstException == null) { this.firstException = exception; } SingletonDictionaryManager <TKey, TInstance> .CloseAsyncResult closeAsyncResult = this; int num = closeAsyncResult.waitCount - 1; int num1 = num; closeAsyncResult.waitCount = num; if (num1 == 0) { flag = true; } } if (flag) { base.Complete(isCompletedSynchronously, this.firstException); } }
protected override IEnumerator <IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult> .AsyncStep> GetAsyncSteps() { while (true) { Monitor.Enter(this.owner.ThisLock); this.shouldReleaseLock = true; try { if (!this.owner.instances.TryGetValue(this.key, out this.singletonContext)) { break; } if (this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loaded) { this.singletonContext.State = SingletonDictionaryManager <TKey, TInstance> .SingletonState.Unloading; this.instance = this.singletonContext.Instance; this.ownsUnloading = true; break; } else if (this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loading || this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Unloading) { SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult> .BeginCall beginCall = (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult; try { thisPtr.shouldReleaseLock = false; asyncResult = thisPtr.owner.BeginWaitPendingOperation(thisPtr.key, t, c, s); } finally { Monitor.Exit(thisPtr.owner.ThisLock); } return(asyncResult); }; yield return(unloadInstanceAsyncResult.CallAsync(beginCall, (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.EndWaitPendingOperation(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); } else { CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] state = new object[] { this.singletonContext.State, typeof(SingletonDictionaryManager <TKey, TInstance>).Name }; string str = string.Format(invariantCulture, "{0} was not recognized. This is likely a bug in {1}.", state); Fx.AssertAndFailFastService(str); break; } } finally { if (this.shouldReleaseLock) { Monitor.Exit(this.owner.ThisLock); } } } if (this.ownsUnloading) { if (!this.waitPendingOperationsCalled) { this.waitPendingOperationsCalled = true; SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult1 = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult> .BeginCall beginCall1 = (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.OnBeginWaitPendingOperations(thisPtr.instance, thisPtr.closeTimeout, c, s); yield return(unloadInstanceAsyncResult1.CallAsync(beginCall1, (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.OnEndWaitPendingOperations(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); } if (!this.shouldAbort) { SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult2 = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult> .BeginCall beginCall2 = (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.OnBeginCloseInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.instance, thisPtr.unloadingContext, thisPtr.closeTimeout, c, s); IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult> .EndCall endCall = (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.OnEndCloseInstance(r); yield return(unloadInstanceAsyncResult2.CallAsync(beginCall2, endCall, (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult thisPtr, TimeSpan t) => thisPtr.owner.OnCloseInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.instance, thisPtr.unloadingContext, thisPtr.closeTimeout), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); } } }
private static void OnFinally(AsyncResult result, Exception exception) { SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult = (SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult)result; if (unloadInstanceAsyncResult.ownsUnloading) { try { if (exception != null || unloadInstanceAsyncResult.shouldAbort) { unloadInstanceAsyncResult.owner.OnAbortInstance(unloadInstanceAsyncResult.singletonContext, unloadInstanceAsyncResult.key, unloadInstanceAsyncResult.instance, unloadInstanceAsyncResult.unloadingContext); } } finally { lock (unloadInstanceAsyncResult.owner.ThisLock) { unloadInstanceAsyncResult.owner.instances.Remove(unloadInstanceAsyncResult.key); } unloadInstanceAsyncResult.owner.SignalPendingOperations(unloadInstanceAsyncResult.key); } } }
public SingletonContext(SingletonDictionaryManager <TKey, TInstance> .SingletonState initialState) { this.State = initialState; }
protected abstract IAsyncResult OnBeginOpenInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, TInstance instance, TimeSpan timeout, AsyncCallback callback, object state);
protected override IEnumerator <IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .AsyncStep> GetAsyncSteps() { while (true) { Monitor.Enter(this.owner.ThisLock); this.shouldReleaseLock = true; try { this.owner.ThrowIfDisposedOrNotOpen(); if (!this.owner.instances.TryGetValue(this.key, out this.singletonContext)) { this.singletonContext = new SingletonDictionaryManager <TKey, TInstance> .SingletonContext(SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loading); this.owner.instances.Add(this.key, this.singletonContext); this.ownsLoading = true; break; } else if (this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loaded) { this.Instance = this.singletonContext.Instance; break; } else if (base.RemainingTime() <= TimeSpan.Zero) { base.Complete(new TimeoutException(SRCore.TimeoutOnOperation(base.OriginalTimeout))); goto Label0; } else if (this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loading || this.singletonContext.State == SingletonDictionaryManager <TKey, TInstance> .SingletonState.Unloading) { SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult loadInstanceAsyncResult = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .BeginCall beginCall = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => { IAsyncResult asyncResult; try { thisPtr.shouldReleaseLock = false; asyncResult = thisPtr.owner.BeginWaitPendingOperation(thisPtr.key, t, c, s); } finally { Monitor.Exit(thisPtr.owner.ThisLock); } return(asyncResult); }; yield return(loadInstanceAsyncResult.CallAsync(beginCall, (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.EndWaitPendingOperation(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); } else { CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] state = new object[] { this.singletonContext.State, typeof(SingletonDictionaryManager <TKey, TInstance>).Name }; string str = string.Format(invariantCulture, "{0} was not recognized. This is likely a bug in {1}.", state); Fx.AssertAndFailFastService(str); break; } } finally { if (this.shouldReleaseLock) { Monitor.Exit(this.owner.ThisLock); } } } if (!this.ownsLoading) { this.owner.OnGetInstance(this.singletonContext, this.key, this.singletonContext.Instance, this.loadingContext); } else { SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult loadInstanceAsyncResult1 = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .BeginCall beginCall1 = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.OnBeginCreateInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.loadingContext, t, c, s); IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .EndCall instance = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.singletonContext.Instance = thisPtr.owner.OnEndCreateInstance(r); yield return(loadInstanceAsyncResult1.CallAsync(beginCall1, instance, (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, TimeSpan t) => thisPtr.singletonContext.Instance = thisPtr.owner.OnCreateInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.loadingContext, t), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); this.created = true; SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult loadInstanceAsyncResult2 = this; IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .BeginCall beginCall2 = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.OnBeginOpenInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.singletonContext.Instance, t, c, s); IteratorAsyncResult <SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult> .EndCall endCall = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.OnEndOpenInstance(r); yield return(loadInstanceAsyncResult2.CallAsync(beginCall2, endCall, (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult thisPtr, TimeSpan t) => thisPtr.owner.OnOpenInstance(thisPtr.singletonContext, thisPtr.key, thisPtr.singletonContext.Instance, t), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer)); } Label0: yield break; }
private static void OnFinally(AsyncResult result, Exception exception) { SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult instance = (SingletonDictionaryManager <TKey, TInstance> .LoadInstanceAsyncResult)result; if (instance.ownsLoading) { if (exception != null || instance.owner.State != CommunicationState.Opened) { try { if (instance.created) { instance.owner.OnAbortInstance(instance.singletonContext, instance.key, instance.singletonContext.Instance, null); } } finally { lock (instance.owner.ThisLock) { instance.owner.instances.Remove(instance.key); } instance.owner.SignalPendingOperations(instance.key); } } else { lock (instance.owner.ThisLock) { instance.Instance = instance.singletonContext.Instance; instance.singletonContext.State = SingletonDictionaryManager <TKey, TInstance> .SingletonState.Loaded; } instance.owner.SignalPendingOperations(instance.key); MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteSingletonManagerLoadSucceeded(instance.key.ToString())); } } }
public GetInstanceAsyncResult(SingletonDictionaryManager <TKey, TInstance> owner, TKey key, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state) { this.owner = owner; this.key = key; this.Instance = default(TInstance); }
protected abstract void OnCloseInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, TInstance instance, object unloadingContext, TimeSpan timeout);
protected abstract TInstance OnCreateInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, object loadingContext, TimeSpan timeout);
protected virtual void OnGetInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, TInstance instance, object loadingContext) { }
protected abstract void OnAbortInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, TInstance instance, object unloadingContext);
protected abstract void OnOpenInstance(SingletonDictionaryManager <TKey, TInstance> .SingletonContext singletonContext, TKey key, TInstance instance, TimeSpan timeout);
protected void UnloadInstance(TKey key, object unloadingContext, bool shouldAbort, TimeSpan timeout) { SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult unloadInstanceAsyncResult = new SingletonDictionaryManager <TKey, TInstance> .UnloadInstanceAsyncResult(this, key, unloadingContext, shouldAbort, timeout, null, null); unloadInstanceAsyncResult.RunSynchronously(); }