static bool HandleEndGetInstance(IAsyncResult result)
            {
                ControlOperationAsyncResult thisPtr = (ControlOperationAsyncResult)result.AsyncState;

                bool shouldAbandon = true;
                try
                {
                    try
                    {
                        thisPtr.workflowServiceInstance = thisPtr.invoker.instanceManager.EndGetInstance(result);
                        shouldAbandon = false;
                    }
                    catch (InstanceLockedException exception)
                    {
                        RedirectionException redirectionException;
                        if (thisPtr.TryCreateRedirectionException(exception, out redirectionException))
                        {
                            throw FxTrace.Exception.AsError(redirectionException);
                        }
                        throw FxTrace.Exception.AsError(CreateFaultException(exception));
                    }
                    catch (OperationCanceledException exception)
                    {
                        thisPtr.BufferReceiveHelper(ref shouldAbandon, true);
                        throw FxTrace.Exception.AsError(new RetryException(null, exception));
                    }
                    catch (InstancePersistenceException exception)
                    {
                        thisPtr.BufferReceiveHelper(ref shouldAbandon, false);

                        if (exception is InstanceKeyNotReadyException)
                        {
                            thisPtr.invoker.host.RaiseUnknownMessageReceived(thisPtr.operationContext.IncomingMessage);
                        }

                        thisPtr.invoker.host.FaultServiceHostIfNecessary(exception);

                        throw FxTrace.Exception.AsError(CreateFaultException(exception));
                    }
                    catch (InstanceUpdateException)
                    {
                        throw FxTrace.Exception.AsError(new FaultException(OperationExecutionFault.CreateUpdateFailedFault(
                            SR.WorkflowInstanceUpdateFailed(thisPtr.instanceId, thisPtr.updatedIdentity.Identity))));
                    }
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    if (!shouldAbandon || !thisPtr.ShouldAbandonReceiveContext())
                    {
                        throw;
                    }
                    return thisPtr.AbandonReceiveContext(exception);
                }

                // When creating a new instance for a normal, keyless app message, create a key for use by context exchange.
                if (!thisPtr.instanceKey.IsValid && thisPtr.instanceId == Guid.Empty)
                {
                    ContextMessageProperty outgoingContextMessageProperty = null;

                    if (!ContextMessageProperty.TryGet(thisPtr.operationContext.OutgoingMessageProperties, out outgoingContextMessageProperty))
                    {
                        outgoingContextMessageProperty = new ContextMessageProperty();
                        outgoingContextMessageProperty.Context.Add(ContextMessageProperty.InstanceIdKey, Guid.NewGuid().ToString());
                        outgoingContextMessageProperty.AddOrReplaceInMessageProperties(thisPtr.operationContext.OutgoingMessageProperties);
                    }
                    else
                    {
                        outgoingContextMessageProperty.Context[ContextMessageProperty.InstanceIdKey] = Guid.NewGuid().ToString();
                    }
                }
                return thisPtr.PerformOperation();
            }
 private static bool HandleEndGetInstance(IAsyncResult result)
 {
     ControlOperationInvoker.ControlOperationAsyncResult asyncState = (ControlOperationInvoker.ControlOperationAsyncResult) result.AsyncState;
     bool shouldAbandon = true;
     try
     {
         try
         {
             asyncState.workflowServiceInstance = asyncState.invoker.instanceManager.EndGetInstance(result);
             shouldAbandon = false;
         }
         catch (InstanceLockedException exception)
         {
             RedirectionException exception2;
             if (asyncState.TryCreateRedirectionException(exception, out exception2))
             {
                 throw System.ServiceModel.Activities.FxTrace.Exception.AsError(exception2);
             }
             throw System.ServiceModel.Activities.FxTrace.Exception.AsError(CreateFaultException(exception));
         }
         catch (OperationCanceledException exception3)
         {
             asyncState.BufferReceiveHelper(ref shouldAbandon, true);
             throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new RetryException(null, exception3));
         }
         catch (InstancePersistenceException exception4)
         {
             asyncState.BufferReceiveHelper(ref shouldAbandon, false);
             if (exception4 is InstanceKeyNotReadyException)
             {
                 asyncState.invoker.host.RaiseUnknownMessageReceived(asyncState.operationContext.IncomingMessage);
             }
             asyncState.invoker.host.FaultServiceHostIfNecessary(exception4);
             throw System.ServiceModel.Activities.FxTrace.Exception.AsError(CreateFaultException(exception4));
         }
     }
     catch (Exception exception5)
     {
         if (Fx.IsFatal(exception5))
         {
             throw;
         }
         if (!shouldAbandon || !asyncState.ShouldAbandonReceiveContext())
         {
             throw;
         }
         return asyncState.AbandonReceiveContext(exception5);
     }
     if (!asyncState.instanceKey.IsValid && (asyncState.instanceId == Guid.Empty))
     {
         ContextMessageProperty contextMessageProperty = null;
         if (!ContextMessageProperty.TryGet(asyncState.operationContext.OutgoingMessageProperties, out contextMessageProperty))
         {
             contextMessageProperty = new ContextMessageProperty();
             contextMessageProperty.Context.Add("instanceId", Guid.NewGuid().ToString());
             contextMessageProperty.AddOrReplaceInMessageProperties(asyncState.operationContext.OutgoingMessageProperties);
         }
         else
         {
             contextMessageProperty.Context["instanceId"] = Guid.NewGuid().ToString();
         }
     }
     return asyncState.PerformOperation();
 }