protected override WorkflowCreationContext OnGetCreationContext(object[] inputs, OperationContext operationContext, Guid instanceId, WorkflowHostingResponseContext responseContext)
 {
     WorkflowCreationContext creationContext = new WorkflowCreationContext();
     creationContext.CreateOnly = true;
     if (operationContext.IncomingMessageHeaders.Action.EndsWith("Create"))
     {
         Dictionary<string, object> arguments = (Dictionary<string, object>)inputs[0];
         if (arguments != null && arguments.Count > 0)
         {
             foreach (KeyValuePair<string, object> pair in arguments)
             {
                 creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
             }
         }
         responseContext.SendResponse(instanceId, null);
     }
     else if (operationContext.IncomingMessageHeaders.Action.EndsWith("CreateWithInstanceId"))
     {
         Dictionary<string, object> arguments = (Dictionary<string, object>)inputs[0];
         if (arguments != null && arguments.Count > 0)
         {
             foreach (KeyValuePair<string, object> pair in arguments)
             {
                 creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
             }
         }
     }
     else
     {
         throw new InvalidOperationException("Invalid Action: " + operationContext.IncomingMessageHeaders.Action);
     }
     return creationContext;
 }
        /// <summary>
        /// Override to create a new<see cref="T:System.ServiceModel.Activities.WorkflowCreationContext" /> instance.
        /// </summary>
        /// <param name="inputs">The inputs to the service operation.</param>
        /// <param name="operationContext">Provides the execution context of the service operation invoked.</param>
        /// <param name="instanceId">The instance ID of the workflow instance being created.</param>
        /// <param name="responseContext">The <see cref="T:System.ServiceModel.Activities.WorkflowHostingEndpointResponseContext" /> object that can be used to send replies back to the message source for a request/reply contract.</param>
        /// <returns>A workflow creation context object.</returns>
        /// <exception cref="InvalidOperationException">Invalid Action:  + operationContext.IncomingMessageHeaders.Action</exception>
        protected override WorkflowCreationContext OnGetCreationContext(object[] inputs, OperationContext operationContext, Guid instanceId, WorkflowHostingResponseContext responseContext)
        {
            var creationContext = new WorkflowCreationContext();
            var action = operationContext.IncomingMessageHeaders.Action;

            if (action.EndsWith("Create"))
            {
                PopulateContextArguments(inputs, creationContext);
                responseContext.SendResponse(instanceId, null);
            }
            else if (action.EndsWith("CreateWithInstanceId"))
            {
                PopulateContextArguments(inputs, creationContext);
            }
            else
            {
                throw new InvalidOperationException("Invalid Action: " + action);
            }

            return creationContext;
        }
        public static WorkflowServiceInstance InitializeInstance(PersistenceContext persistenceContext, Guid instanceId, Activity workflowDefinition, WorkflowIdentity definitionIdentity, IDictionary<XName, InstanceValue> loadedObject, WorkflowCreationContext creationContext,
            SynchronizationContext synchronizationContext, WorkflowServiceHost serviceHost, DynamicUpdateMap updateMap = null)
        {
            Fx.Assert(workflowDefinition != null, "workflowDefinition cannot be null.");
            Fx.Assert(serviceHost != null, "serviceHost cannot be null!");
            Fx.Assert(instanceId != Guid.Empty, "instanceId cannot be empty.");

            WorkflowServiceInstance workflowInstance = new WorkflowServiceInstance(workflowDefinition, definitionIdentity, instanceId, serviceHost, persistenceContext)
            {
                SynchronizationContext = synchronizationContext
            };

            // let us initalize the instance level extensions here
            workflowInstance.SetupExtensions(serviceHost.WorkflowExtensions);

            if (loadedObject != null)
            {
                InstanceValue stateValue;
                object deserializedRuntimeState;

                if (!loadedObject.TryGetValue(WorkflowNamespace.Workflow, out stateValue) || stateValue.Value == null)
                {
                    throw FxTrace.Exception.AsError(
                        new InstancePersistenceException(SR.WorkflowInstanceNotFoundInStore(instanceId)));
                }
                deserializedRuntimeState = stateValue.Value;

                if (loadedObject.TryGetValue(WorkflowServiceNamespace.CreationContext, out stateValue))
                {
                    workflowInstance.creationContext = (WorkflowCreationContext)stateValue.Value;
                }

                if (persistenceContext.IsSuspended)
                {
                    workflowInstance.state = State.Suspended;
                }
                try
                {
                    workflowInstance.Initialize(deserializedRuntimeState, updateMap);
                }
                catch (InstanceUpdateException)
                {
                    // Need to flush the tracking record for the update failure
                    workflowInstance.ScheduleAbortTracking(true);
                    throw;
                }

                if (updateMap != null)
                {
                    workflowInstance.HasBeenUpdated = true;
                }
            }
            else
            {
                IList<Handle> rootExecutionProperties = null;
                IDictionary<string, object> workflowArguments = null;
                // Provide default CorrelationScope if root activity is not CorrelationScope
                if (!(workflowDefinition is CorrelationScope))
                {
                    rootExecutionProperties = new List<Handle>(1)
                    {
                        new CorrelationHandle()
                    };
                }

                if (creationContext != null)
                {
                    workflowArguments = creationContext.RawWorkflowArguments;
                    workflowInstance.creationContext = creationContext;
                }
                workflowInstance.Initialize(workflowArguments, rootExecutionProperties);
            }

            return workflowInstance;
        }
        private static void PopulateContextArguments(IReadOnlyList<object> inputs, WorkflowCreationContext creationContext)
        {
            var arguments = (Dictionary<string, object>)inputs[0];
            if (arguments == null) return;

            foreach (var pair in arguments)
                creationContext.WorkflowArguments.Add(pair.Key, pair.Value);
        }
 public WorkflowServiceInstance InitializeInstance(Guid instanceId, PersistenceContext context, IDictionary<XName, InstanceValue> instance, WorkflowCreationContext creationContext)
 {
     return WorkflowServiceInstance.InitializeInstance(context, instanceId, this.workflowDefinition, instance, creationContext, WorkflowSynchronizationContext.Instance, this.serviceHost);
 }
 public static WorkflowServiceInstance InitializeInstance(PersistenceContext persistenceContext, Guid instanceId, Activity workflowDefinition, IDictionary<XName, InstanceValue> loadedObject, WorkflowCreationContext creationContext, SynchronizationContext synchronizationContext, WorkflowServiceHost serviceHost)
 {
     WorkflowServiceInstance instance = new WorkflowServiceInstance(workflowDefinition, instanceId, serviceHost, persistenceContext) {
         SynchronizationContext = synchronizationContext
     };
     instance.SetupExtensions(serviceHost.WorkflowExtensions);
     if (loadedObject != null)
     {
         InstanceValue value2;
         if (!loadedObject.TryGetValue(WorkflowNamespace.Workflow, out value2) || (value2.Value == null))
         {
             throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InstancePersistenceException(System.ServiceModel.Activities.SR.WorkflowInstanceNotFoundInStore(instanceId)));
         }
         object deserializedRuntimeState = value2.Value;
         if (loadedObject.TryGetValue(WorkflowServiceNamespace.CreationContext, out value2))
         {
             instance.creationContext = (WorkflowCreationContext) value2.Value;
         }
         if (persistenceContext.IsSuspended)
         {
             instance.state = State.Suspended;
         }
         instance.Initialize(deserializedRuntimeState);
         return instance;
     }
     IList<Handle> workflowExecutionProperties = null;
     IDictionary<string, object> workflowArgumentValues = null;
     if (!(workflowDefinition is CorrelationScope))
     {
         workflowExecutionProperties = new List<Handle>(1) {
             new CorrelationHandle()
         };
     }
     if (creationContext != null)
     {
         workflowArgumentValues = creationContext.RawWorkflowArguments;
         instance.creationContext = creationContext;
     }
     instance.Initialize(workflowArgumentValues, workflowExecutionProperties);
     return instance;
 }