void EnsureInstanceIdAndIdentity()
 {
     if (this.invoker.isControlOperation)
     {
         switch (this.invoker.operationName)
         {
             case XD2.WorkflowInstanceManagementService.Abandon:
             case XD2.WorkflowInstanceManagementService.Cancel:
             case XD2.WorkflowInstanceManagementService.TransactedCancel:
             case XD2.WorkflowInstanceManagementService.Run:
             case XD2.WorkflowInstanceManagementService.TransactedRun:
             case XD2.WorkflowInstanceManagementService.Suspend:
             case XD2.WorkflowInstanceManagementService.TransactedSuspend:
             case XD2.WorkflowInstanceManagementService.Terminate:
             case XD2.WorkflowInstanceManagementService.TransactedTerminate:
             case XD2.WorkflowInstanceManagementService.Unsuspend:
             case XD2.WorkflowInstanceManagementService.TransactedUnsuspend:
                 this.instanceId = GetInstanceIdForControlOperation(this.inputs);
                 break;
             case XD2.WorkflowInstanceManagementService.Update:
             case XD2.WorkflowInstanceManagementService.TransactedUpdate:
                 this.instanceId = GetInstanceIdForControlOperation(this.inputs);
                 this.updatedIdentity = new WorkflowIdentityKey(GetIdentityForControlOperation(this.inputs));
                 break;
             default:
                 throw Fx.AssertAndThrow("Unreachable code");
         }
     }
     else if (this.invoker.endpoint is WorkflowHostingEndpoint)
     {
         WorkflowHostingEndpoint hostingEndpoint = (WorkflowHostingEndpoint)this.invoker.endpoint;
         this.instanceId = hostingEndpoint.OnGetInstanceId(inputs, this.operationContext);
         if (this.instanceId == Guid.Empty)
         {
             this.invoker.GetInstanceKeys(this.operationContext, out this.instanceKey, out this.additionalKeys);
         }
     }
     else
     {
         //User endpoint operation.
         this.invoker.GetInstanceKeys(this.operationContext, out this.instanceKey, out this.additionalKeys);
     }
 }
 internal IAsyncResult BeginGetInstance(Guid instanceId, WorkflowGetInstanceContext parameters,
     WorkflowIdentityKey updatedIdentity, TimeSpan timeout, AsyncCallback callback, object state)
 {
     ThrowIfClosedOrAborted(this.state);
     return new GetInstanceAsyncResult(this, instanceId, parameters, updatedIdentity, timeout, callback, state);
 }
            public GetInstanceAsyncResult(DurableInstanceManager instanceManager, Guid instanceId, WorkflowGetInstanceContext parameters, WorkflowIdentityKey updatedIdentity,
                TimeSpan timeout, AsyncCallback callback, object state)
                : this(instanceManager, parameters, timeout, callback, state)
            {
                this.instanceId = instanceId;
                this.updatedIdentity = updatedIdentity;

                if (this.GetInstance())
                {
                    this.Complete(true);
                }
            }
            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 IAsyncResult BeginLoad(Guid instanceId, ICollection<InstanceKey> associatedKeys, Transaction transaction, bool loadAny, WorkflowIdentityKey updatedIdentity,
     TimeSpan timeout, AsyncCallback callback, object state)
 {
     if (instanceId == Guid.Empty && !loadAny)
     {
         throw FxTrace.Exception.Argument("instanceId", SR.InvalidInstanceId);
     }
     Fx.Assert(!loadAny || instanceId == Guid.Empty, "instanceId must be Empty for loadAny!");
     return new LoadOrCreateAsyncResult(this, null, instanceId, false,
         associatedKeys, transaction, loadAny, updatedIdentity, timeout, callback, state);
 }
        public WorkflowServiceInstance InitializeInstance(Guid instanceId, PersistenceContext context, WorkflowIdentity definitionIdentity, WorkflowIdentityKey updatedIdentity, IDictionary<XName, InstanceValue> instance, WorkflowCreationContext creationContext)
        {
            Activity workflowDefinition = null;
            DynamicUpdateMap updateMap = null;
            if (updatedIdentity != null && !object.Equals(updatedIdentity.Identity, definitionIdentity))
            {
                if (!this.workflowDefinitionProvider.TryGetDefinitionAndMap(definitionIdentity, updatedIdentity.Identity, out workflowDefinition, out updateMap))
                {
                    if (this.workflowDefinitionProvider.TryGetDefinition(updatedIdentity.Identity, out workflowDefinition))
                    {
                        throw FxTrace.Exception.AsError(new FaultException(
                            OperationExecutionFault.CreateUpdateFailedFault(SR.UpdateMapNotFound(definitionIdentity, updatedIdentity.Identity))));
                    }
                    else
                    {
                        throw FxTrace.Exception.AsError(new FaultException(
                            OperationExecutionFault.CreateUpdateFailedFault(SR.UpdateDefinitionNotFound(updatedIdentity.Identity))));
                    }
                }
            }
            else if (!this.workflowDefinitionProvider.TryGetDefinition(definitionIdentity, out workflowDefinition))
            {
                throw FxTrace.Exception.AsError(new VersionMismatchException(SR.WorkflowServiceDefinitionIdentityNotMatched(definitionIdentity), null, definitionIdentity));
            }

            WorkflowIdentity definitionToLoad = updatedIdentity == null ? definitionIdentity : updatedIdentity.Identity;
            return WorkflowServiceInstance.InitializeInstance(context, instanceId, workflowDefinition, definitionToLoad, instance, creationContext,
                WorkflowSynchronizationContext.Instance, this.serviceHost, updateMap);
        }