void StartOperation()
        {
            Guid instanceId = (this.InstancePersistenceContext != null) ? this.InstancePersistenceContext.InstanceView.InstanceId : Guid.Empty;
            Exception delayedException = null;

            try
            {
                this.sqlCommand = new SqlCommand();
                this.GenerateSqlCommand(this.sqlCommand);
                this.sqlCommand.CommandText = this.GetSqlCommandText();
                this.sqlCommand.CommandType = this.GetSqlCommandType();

                StoreUtilities.TraceSqlCommand(this.sqlCommand, true);
                SqlCommandAsyncResult sqlCommandResult = new SqlCommandAsyncResult(this.sqlCommand, this.ConnectionString,
                    (this.InstancePersistenceContext != null) ? this.InstancePersistenceContext.EventTraceActivity : null,
                    this.DependentTransaction, this.TimeoutHelper.RemainingTime(), 0, this.maximumRetries, PrepareAsyncCompletion(onSqlCommandAsyncResultCallback), this);
                sqlCommandResult.StartCommand();

                if (!SyncContinue(sqlCommandResult))
                {
                    return;
                }
            }
            catch (InstancePersistenceException instancePersistenceException)
            {
                delayedException = instancePersistenceException;
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                delayedException = new InstancePersistenceCommandException(this.InstancePersistenceCommand.Name, instanceId, exception);
            }

            if (delayedException != null)
            {
                if (this.sqlCommand.Connection != null)
                {
                    this.sqlCommand.Connection.Close();
                }

                this.sqlCommand.Dispose();
                this.TraceException(delayedException);
            }

            this.Complete(false, delayedException);
        }
        static bool SqlCommandAsyncResultCallback(IAsyncResult result)
        {
            SqlWorkflowInstanceStoreAsyncResult thisPtr = (SqlWorkflowInstanceStoreAsyncResult)result.AsyncState;
            Exception delayedException = null;
            bool completeFlag = true;

            try
            {
                using (thisPtr.sqlCommand)
                {
                    using (SqlDataReader reader = SqlCommandAsyncResult.End(result))
                    {
                        delayedException = thisPtr.ProcessSqlResult(reader);
                    }
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                Guid instanceId = (thisPtr.InstancePersistenceContext != null) ? thisPtr.InstancePersistenceContext.InstanceView.InstanceId : Guid.Empty;
                delayedException = new InstancePersistenceCommandException(thisPtr.InstancePersistenceCommand.Name, instanceId, exception);
            }

            if (delayedException is InstanceAlreadyLockedToOwnerException)
            {
                InstanceAlreadyLockedToOwnerException alreadyLockedException = (InstanceAlreadyLockedToOwnerException) delayedException;
                long reclaimLockAtVersion = alreadyLockedException.InstanceVersion;

                if (!thisPtr.InstancePersistenceContext.InstanceView.IsBoundToInstance)
                {
                    thisPtr.InstancePersistenceContext.BindInstance(alreadyLockedException.InstanceId);
                }

                IAsyncResult bindReclaimedAsyncResult = thisPtr.InstancePersistenceContext.BeginBindReclaimedLock(reclaimLockAtVersion, thisPtr.TimeoutHelper.RemainingTime(), thisPtr.PrepareAsyncCompletion(SqlWorkflowInstanceStoreAsyncResult.onBindReclaimed), thisPtr);

                if (!thisPtr.SyncContinue(bindReclaimedAsyncResult))
                {
                    completeFlag = false;
                }
            }
            else if (delayedException != null)
            {
                if (thisPtr.sqlCommand.Connection != null)
                {
                    thisPtr.sqlCommand.Connection.Close();
                }

                bool handled = false;
                thisPtr.OnSqlException(delayedException, out handled);

                if (!handled)
                {
                    thisPtr.TraceException(delayedException);

                    throw FxTrace.Exception.AsError(delayedException);
                }
            }

            if (completeFlag)
            {
                completeFlag = thisPtr.OnSqlProcessingComplete();
            }

            return completeFlag;
        }
 private static bool SqlCommandAsyncResultCallback(IAsyncResult result)
 {
     SqlWorkflowInstanceStoreAsyncResult asyncState = (SqlWorkflowInstanceStoreAsyncResult) result.AsyncState;
     Exception exception = null;
     bool flag = true;
     try
     {
         using (asyncState.sqlCommand)
         {
             using (SqlDataReader reader = SqlCommandAsyncResult.End(result))
             {
                 exception = asyncState.ProcessSqlResult(reader);
             }
         }
     }
     catch (Exception exception2)
     {
         if (Fx.IsFatal(exception2))
         {
             throw;
         }
         Guid instanceId = (asyncState.InstancePersistenceContext != null) ? asyncState.InstancePersistenceContext.InstanceView.InstanceId : Guid.Empty;
         exception = new InstancePersistenceCommandException(asyncState.InstancePersistenceCommand.Name, instanceId, exception2);
     }
     if (exception is InstanceAlreadyLockedToOwnerException)
     {
         InstanceAlreadyLockedToOwnerException exception3 = (InstanceAlreadyLockedToOwnerException) exception;
         long instanceVersion = exception3.InstanceVersion;
         if (!asyncState.InstancePersistenceContext.InstanceView.IsBoundToInstance)
         {
             asyncState.InstancePersistenceContext.BindInstance(exception3.InstanceId);
         }
         IAsyncResult result3 = asyncState.InstancePersistenceContext.BeginBindReclaimedLock(instanceVersion, asyncState.TimeoutHelper.RemainingTime(), asyncState.PrepareAsyncCompletion(onBindReclaimed), asyncState);
         if (!asyncState.SyncContinue(result3))
         {
             flag = false;
         }
         return flag;
     }
     if (exception == null)
     {
         return flag;
     }
     if (asyncState.sqlCommand.Connection != null)
     {
         asyncState.sqlCommand.Connection.Close();
     }
     asyncState.TraceException(exception);
     throw FxTrace.Exception.AsError(exception);
 }
        public static Exception GetError(XName commandName, CommandResult result, SqlDataReader reader)
        {
            Exception returnValue = null;

            if (result != CommandResult.Success)
            {
                switch (result)
                {
                    case CommandResult.InstanceAlreadyExists:
                        returnValue = new InstanceCollisionException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.InstanceLockNotAcquired:
                        returnValue = new InstanceLockedException(commandName, reader.GetGuid(1), reader.GetGuid(2), ReadLockOwnerMetadata(reader));
                        break;
                    case CommandResult.InstanceNotFound:
                        returnValue = new InstanceNotReadyException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.KeyAlreadyExists:
                        returnValue = new InstanceKeyCollisionException(commandName, Guid.Empty,
                            new InstanceKey(reader.GetGuid(1)), Guid.Empty);
                        break;
                    case CommandResult.KeyNotFound:
                        returnValue = new InstanceKeyNotReadyException(commandName, new InstanceKey(reader.GetGuid(1)));
                        break;
                    case CommandResult.InstanceLockLost:
                        returnValue = new InstanceLockLostException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.InstanceCompleted:
                        returnValue = new InstanceCompleteException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.KeyDisassociated:
                        returnValue = new InstanceKeyCompleteException(commandName, new InstanceKey(reader.GetGuid(1)));
                        break;
                    case CommandResult.StaleInstanceVersion:
                        returnValue = new InstanceLockLostException(commandName, reader.GetGuid(1));
                        break;
                    case CommandResult.HostLockExpired:
                        returnValue = new InstancePersistenceException(SR.HostLockExpired);
                        break;
                    case CommandResult.HostLockNotFound:
                        returnValue = new InstancePersistenceException(SR.HostLockNotFound);
                        break;
                    case CommandResult.CleanupInProgress:
                        returnValue = new InstancePersistenceCommandException(SR.CleanupInProgress);
                        break;
                    case CommandResult.InstanceAlreadyLockedToOwner:
                        returnValue = new InstanceAlreadyLockedToOwnerException(commandName, reader.GetGuid(1), reader.GetInt64(2));
                        break;
                    default:
                        returnValue = new InstancePersistenceCommandException(SR.UnknownSprocResult(result));
                        break;
                }
            }

            return returnValue;
        }
 private void StartOperation()
 {
     Guid instanceId = (this.InstancePersistenceContext != null) ? this.InstancePersistenceContext.InstanceView.InstanceId : Guid.Empty;
     Exception exception = null;
     try
     {
         this.sqlCommand = new SqlCommand();
         this.GenerateSqlCommand(this.sqlCommand);
         this.sqlCommand.CommandText = this.GetSqlCommandText();
         this.sqlCommand.CommandType = this.GetSqlCommandType();
         StoreUtilities.TraceSqlCommand(this.sqlCommand, true);
         SqlCommandAsyncResult result = new SqlCommandAsyncResult(this.sqlCommand, this.ConnectionString, this.DependentTransaction, this.TimeoutHelper.RemainingTime(), 0, this.Store.MaxConnectionRetries, base.PrepareAsyncCompletion(onSqlCommandAsyncResultCallback), this);
         result.StartCommand();
         if (!base.SyncContinue(result))
         {
             return;
         }
     }
     catch (InstancePersistenceException exception2)
     {
         exception = exception2;
     }
     catch (Exception exception3)
     {
         if (Fx.IsFatal(exception3))
         {
             throw;
         }
         exception = new InstancePersistenceCommandException(this.InstancePersistenceCommand.Name, instanceId, exception3);
     }
     if (exception != null)
     {
         if (this.sqlCommand.Connection != null)
         {
             this.sqlCommand.Connection.Close();
         }
         this.sqlCommand.Dispose();
         this.TraceException(exception);
     }
     base.Complete(false, exception);
 }