public ReserveThrottleAsyncResult(PersistenceProviderDirectory directory, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
 {
     if (directory.throttle.EnterAsync(timeout, onThrottleAcquired, this))
     {
         this.ownsThrottle = true;
         base.Complete(true);
     }
 }
            public LoadOrCreateAsyncResult(PersistenceProviderDirectory ppd, InstanceKey key, Guid suggestedIdOrId, bool canCreateInstance, ICollection <InstanceKey> associatedKeys, Transaction transaction, bool loadAny, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
            {
                bool flag;

                this.ppd               = ppd;
                this.key               = key;
                this.suggestedIdOrId   = suggestedIdOrId;
                this.canCreateInstance = canCreateInstance;
                this.associatedKeys    = associatedKeys;
                this.transaction       = transaction;
                this.loadAny           = loadAny;
                this.timeoutHelper     = new TimeoutHelper(timeout);
                if ((this.associatedKeys != null) && (this.associatedKeys.Count == 0))
                {
                    this.associatedKeys = null;
                }
                base.OnCompleting = onComplete;
                if (this.transaction != null)
                {
                    TransactionInterop.GetDtcTransaction(this.transaction);
                }
                Exception exception = null;

                try
                {
                    this.result = this.loadAny ? null : this.ppd.LoadFromCache(this.key, this.suggestedIdOrId, this.canCreateInstance);
                    if (this.result != null)
                    {
                        flag = true;
                    }
                    else
                    {
                        if ((this.ppd.store == null) && !this.canCreateInstance)
                        {
                            if (this.key != null)
                            {
                                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InstanceKeyNotReadyException(null, this.key));
                            }
                            throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InstanceNotReadyException(null, this.suggestedIdOrId));
                        }
                        IAsyncResult result = this.ppd.BeginReserveThrottle(this.timeoutHelper.RemainingTime(), base.PrepareAsyncCompletion(handleReserveThrottle), this);
                        flag = base.SyncContinue(result);
                    }
                }
                catch (Exception exception2)
                {
                    if (Fx.IsFatal(exception2))
                    {
                        throw;
                    }
                    exception = exception2;
                    flag      = true;
                }
                if (flag)
                {
                    base.Complete(true, exception);
                }
            }
 internal PersistenceContext(PersistenceProviderDirectory directory, Guid instanceId, InstanceKey key, IEnumerable <InstanceKey> associatedKeys)
 {
     this.directory      = directory;
     this.InstanceId     = instanceId;
     this.AssociatedKeys = (associatedKeys != null) ? new HashSet <InstanceKey>(associatedKeys) : new HashSet <InstanceKey>();
     if ((key != null) && !this.AssociatedKeys.Contains(key))
     {
         this.AssociatedKeys.Add(key);
     }
     this.keysToAssociate        = new HashSet <InstanceKey>(this.AssociatedKeys);
     this.keysToDisassociate     = new HashSet <InstanceKey>();
     this.lockingTransaction     = 0;
     this.Detaching              = false;
     this.transactionWaiterQueue = new Queue <TransactionWaitAsyncResult>();
 }
 internal PersistenceContext(PersistenceProviderDirectory directory, InstanceStore store, InstanceHandle handle, Guid instanceId, IEnumerable <InstanceKey> associatedKeys, bool newInstance, bool locked, InstanceView view) : this(directory, instanceId, null, associatedKeys)
 {
     this.store         = store;
     this.handle        = handle;
     this.IsInitialized = !newInstance;
     this.IsLocked      = locked;
     if (view != null)
     {
         this.ReadSuspendedInfo(view);
     }
     if (this.IsInitialized || this.IsLocked)
     {
         this.RationalizeSavedKeys(false);
     }
     if (this.IsInitialized)
     {
         this.workflowInstance = this.directory.InitializeInstance(this.InstanceId, this, view.InstanceData, null);
     }
 }
        void InitializePersistenceProviderDirectory()
        {   
            int maxInstances = ServiceThrottlingBehavior.DefaultMaxConcurrentInstances;
            ServiceThrottlingBehavior serviceThrottlingBehavior = Host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
            if (serviceThrottlingBehavior != null)
            {
                maxInstances = serviceThrottlingBehavior.MaxConcurrentInstances;
            }

            if (InstanceStore != null)
            {
                PersistenceProviderDirectory = new PersistenceProviderDirectory(InstanceStore, this.owner, this.instanceMetadataChanges, this.workflowDefinitionProvider, Host, DurableConsistencyScope.Global, maxInstances);
            }
            else
            {
                PersistenceProviderDirectory = new PersistenceProviderDirectory(this.workflowDefinitionProvider, Host, maxInstances);
            }

            bool aborted;
            lock (this.thisLock)
            {
                aborted = this.state == States.Aborted;
            }

            if (aborted)
            {
                if (this.handle != null)
                {
                    this.handle.Free();
                }

                PersistenceProviderDirectory.Abort();
            }

            // Start listening to store event
            if (InstanceStore != null && !aborted)
            {
                this.waitForStoreEventsLoop = new AsyncWaitHandle(EventResetMode.ManualReset);
                BeginWaitAndHandleStoreEvents(waitAndHandleStoreEventsCallback, this);
            }
        }
            public LoadOrCreateAsyncResult(PersistenceProviderDirectory ppd, InstanceKey key, Guid suggestedIdOrId,
                bool canCreateInstance, ICollection<InstanceKey> associatedKeys, Transaction transaction, bool loadAny, WorkflowIdentityKey updatedIdentity,
                TimeSpan timeout, AsyncCallback callback, object state)
                : base(callback, state)
            {
                Exception completionException = null;
                bool completeSelf = false;

                this.ppd = ppd;
                this.key = key;
                this.suggestedIdOrId = suggestedIdOrId;
                this.canCreateInstance = canCreateInstance;
                this.associatedKeys = associatedKeys;
                Fx.Assert(!ppd.serviceHost.IsLoadTransactionRequired || (transaction != null), "Transaction must exist!");
                this.transaction = transaction;
                this.loadAny = loadAny;
                this.updatedIdentity = updatedIdentity;
                this.timeoutHelper = new TimeoutHelper(timeout);

                if (this.associatedKeys != null && this.associatedKeys.Count == 0)
                {
                    this.associatedKeys = null;
                }

                OnCompleting = LoadOrCreateAsyncResult.onComplete;

                //
                if (this.transaction != null)
                {
                    LoadOrCreateAsyncResult.PromoteTransaction(this.transaction);
                }

                try
                {
                    if (this.LoadFromCache())
                    {
                        completeSelf = true;
                    }
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    completionException = exception;
                    completeSelf = true;
                }

                if (completeSelf)
                {
                    this.Complete(true, completionException);
                }
            }
 public ReserveThrottleAsyncResult(PersistenceProviderDirectory directory, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.ppd = directory;
     if (directory.throttle.EnterAsync(timeout, ReserveThrottleAsyncResult.onThrottleAcquired, this))
     {
         this.ownsThrottle = true;
         this.ppd.serviceHost.WorkflowServiceHostPerformanceCounters.WorkflowInMemory();
         Complete(true);
     }
 }
 public ReserveThrottleAsyncResult(PersistenceProviderDirectory directory, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
 {
     if (directory.throttle.EnterAsync(timeout, onThrottleAcquired, this))
     {
         this.ownsThrottle = true;
         base.Complete(true);
     }
 }
 public LoadOrCreateAsyncResult(PersistenceProviderDirectory ppd, InstanceKey key, Guid suggestedIdOrId, bool canCreateInstance, ICollection<InstanceKey> associatedKeys, Transaction transaction, bool loadAny, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
 {
     bool flag;
     this.ppd = ppd;
     this.key = key;
     this.suggestedIdOrId = suggestedIdOrId;
     this.canCreateInstance = canCreateInstance;
     this.associatedKeys = associatedKeys;
     this.transaction = transaction;
     this.loadAny = loadAny;
     this.timeoutHelper = new TimeoutHelper(timeout);
     if ((this.associatedKeys != null) && (this.associatedKeys.Count == 0))
     {
         this.associatedKeys = null;
     }
     base.OnCompleting = onComplete;
     if (this.transaction != null)
     {
         TransactionInterop.GetDtcTransaction(this.transaction);
     }
     Exception exception = null;
     try
     {
         this.result = this.loadAny ? null : this.ppd.LoadFromCache(this.key, this.suggestedIdOrId, this.canCreateInstance);
         if (this.result != null)
         {
             flag = true;
         }
         else
         {
             if ((this.ppd.store == null) && !this.canCreateInstance)
             {
                 if (this.key != null)
                 {
                     throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InstanceKeyNotReadyException(null, this.key));
                 }
                 throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InstanceNotReadyException(null, this.suggestedIdOrId));
             }
             IAsyncResult result = this.ppd.BeginReserveThrottle(this.timeoutHelper.RemainingTime(), base.PrepareAsyncCompletion(handleReserveThrottle), this);
             flag = base.SyncContinue(result);
         }
     }
     catch (Exception exception2)
     {
         if (Fx.IsFatal(exception2))
         {
             throw;
         }
         exception = exception2;
         flag = true;
     }
     if (flag)
     {
         base.Complete(true, exception);
     }
 }