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);
        }
        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;
        }
        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);
        }