예제 #1
0
        private bool LoadWorkflow(InstancePersistenceContext context, LoadWorkflowCommand command)
        {
            if (command.AcceptUninitializedInstance)
            {
                return(false);
            }

            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            Guid instanceId      = context.InstanceView.InstanceId;
            Guid instanceOwnerId = context.InstanceView.InstanceOwner.InstanceOwnerId;

            IDictionary <XName, InstanceValue> instanceData     = null;
            IDictionary <XName, InstanceValue> instanceMetadata = null;

            Dictionary <string, object> fullInstanceData = _stores.LoadWorkflowContext();

            instanceData     = this.DeserializePropertyBag((Dictionary <XName, object>)fullInstanceData["instanceData"]);
            instanceMetadata = this.DeserializePropertyBag((Dictionary <XName, object>)fullInstanceData["instanceMetadata"]);

            context.LoadedInstance(InstanceState.Initialized, instanceData, instanceMetadata, null, null);

            return(true);
        }
예제 #2
0
        protected override IAsyncResult BeginTryCommand(
            InstancePersistenceContext context,
            InstancePersistenceCommand command,
            TimeSpan timeout, AsyncCallback callback,
            object state)
        {
            try
            {
                switch (command)
                {
                case SaveWorkflowCommand saveWorkflowCommand:
                    return(new TypedCompletedAsyncResult <bool>(SaveWorkflow(context, saveWorkflowCommand), callback,
                                                                state));

                case LoadWorkflowCommand loadWorkflowCommand:
                    return(new TypedCompletedAsyncResult <bool>(LoadWorkflow(context, loadWorkflowCommand), callback,
                                                                state));

                case CreateWorkflowOwnerCommand createWorkflowOwnerCommand:
                    return(new TypedCompletedAsyncResult <bool>(
                               CreateWorkflowOwner(context, createWorkflowOwnerCommand), callback, state));

                case DeleteWorkflowOwnerCommand deleteWorkflowOwnerCommand:
                    return(new TypedCompletedAsyncResult <bool>(
                               DeleteWorkflowOwner(context, deleteWorkflowOwnerCommand), callback, state));

                default:
                    return(new TypedCompletedAsyncResult <bool>(false, callback, state));
                }
            }
            catch (Exception e)
            {
                return(new TypedCompletedAsyncResult <Exception>(e, callback, state));
            }
        }
예제 #3
0
 protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
 {
     try
     {
         if (command is SaveWorkflowCommand)
         {
             return(new TypedCompletedAsyncResult <bool>(SaveWorkflow(context, (SaveWorkflowCommand)command), callback, state));
         }
         else if (command is LoadWorkflowCommand)
         {
             return(new TypedCompletedAsyncResult <bool>(LoadWorkflow(context, (LoadWorkflowCommand)command), callback, state));
         }
         else if (command is CreateWorkflowOwnerCommand)
         {
             return(new TypedCompletedAsyncResult <bool>(CreateWorkflowOwner(context, (CreateWorkflowOwnerCommand)command), callback, state));
         }
         else if (command is CreateWorkflowOwnerWithIdentityCommand)
         {
             return(new TypedCompletedAsyncResult <bool>(CreateWorkflowOwnerWithIdentity(context, (CreateWorkflowOwnerWithIdentityCommand)command), callback, state));
         }
         else if (command is DeleteWorkflowOwnerCommand)
         {
             return(new TypedCompletedAsyncResult <bool>(DeleteWorkflowOwner(context, (DeleteWorkflowOwnerCommand)command), callback, state));
         }
         return(new TypedCompletedAsyncResult <bool>(false, callback, state));
     }
     catch (Exception e)
     {
         return(new TypedCompletedAsyncResult <Exception>(e, callback, state));
     }
 }
 private Exception ProcessLoadWorkflowByInstanceKey(
     InstancePersistenceContext context,
     LoadWorkflowByInstanceKeyCommand command)
 {
     try
     {
         Guid instanceId = _dataStore.GetInstanceAssociation(
             command.LookupInstanceKey);
         if (instanceId == Guid.Empty)
         {
             throw new InstanceKeyNotReadyException(
                       String.Format("Unable to load instance for key: {0}",
                                     command.LookupInstanceKey));
         }
         SharedLoadWorkflow(context, instanceId);
         return(null);
     }
     catch (InstancePersistenceException exception)
     {
         Console.WriteLine(
             "ProcessLoadWorkflowByInstanceKey exception: {0}",
             exception.Message);
         return(exception);
     }
 }
예제 #5
0
 /// <summary>
 /// Handles a <see cref="InstancePersistenceCommand"/>.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="command"></param>
 /// <returns></returns>
 Task <bool> CommandAsync(InstancePersistenceContext context, InstancePersistenceCommand command)
 {
     if (command is CreateWorkflowOwnerCommand)
     {
         return(CreateWorkflowOwnerCommand(context, (CreateWorkflowOwnerCommand)command));
     }
     else if (command is QueryActivatableWorkflowsCommand)
     {
         return(QueryActivatableWorkflowsCommand(context, (QueryActivatableWorkflowsCommand)command));
     }
     else if (command is SaveWorkflowCommand)
     {
         return(SaveWorkflowCommand(context, (SaveWorkflowCommand)command));
     }
     else if (command is LoadWorkflowCommand)
     {
         return(LoadWorkflowCommand(context, (LoadWorkflowCommand)command));
     }
     else if (command is TryLoadRunnableWorkflowCommand)
     {
         return(TryLoadRunnableWorkflowCommand(context, (TryLoadRunnableWorkflowCommand)command));
     }
     else if (command is DeleteWorkflowOwnerCommand)
     {
         return(DeleteWorkflowOwnerCommand(context, (DeleteWorkflowOwnerCommand)command));
     }
     else
     {
         return(Task.FromResult(true));
     }
 }
 /// <summary>
 /// The try command.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="command">The command.</param>
 /// <param name="timeout">The timeout.</param>
 /// <returns>The <see cref="bool" />.</returns>
 protected override bool TryCommand(
     InstancePersistenceContext context,
     InstancePersistenceCommand command,
     TimeSpan timeout)
 {
     return(this.EndTryCommand(this.BeginTryCommand(context, command, timeout, null, null)));
 }
 private Exception ProcessLoadWorkflow(
     InstancePersistenceContext context,
     LoadWorkflowCommand command)
 {
     try
     {
         if (command.AcceptUninitializedInstance)
         {
             context.LoadedInstance(InstanceState.Uninitialized,
                                    null, null, null, null);
         }
         else
         {
             SharedLoadWorkflow(context, context.InstanceView.InstanceId);
         }
         return(null);
     }
     catch (InstancePersistenceException exception)
     {
         Console.WriteLine(
             // ReSharper disable LocalizableElement
             "ProcessLoadWorkflow exception: {0}",
             // ReSharper restore LocalizableElement
             exception.Message);
         return(exception);
     }
 }
        private Exception ProcessLoadWorkflowByInstanceKey(
            InstancePersistenceContext context,
            LoadWorkflowByInstanceKeyCommand command)
        {
            try
            {
                Guid instanceId = _dataStore.GetInstanceAssociation(
                    command.LookupInstanceKey);
                if (instanceId == Guid.Empty)
                {
                    throw new InstanceKeyNotReadyException(
                              String.Format(ErrorResource.UnableToLoadInstance,
                                            command.LookupInstanceKey));
                }

                SharedLoadWorkflow(context, instanceId);
                return(null);
            }
            catch (InstancePersistenceException exception)
            {
                Console.WriteLine(
                    // ReSharper disable LocalizableElement
                    "ProcessLoadWorkflowByInstanceKey exception: {0}",
                    // ReSharper restore LocalizableElement
                    exception.Message);
                return(exception);
            }
        }
예제 #9
0
        internal IAsyncResult BeginTryCommandInternalWithVersionCheck(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state, Version targetVersion)
        {
            SqlWorkflowInstanceStoreAsyncResult sqlWorkflowInstanceStoreAsyncResult = new TestDatabaseVersionAndRunAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, targetVersion, callback, state);

            sqlWorkflowInstanceStoreAsyncResult.ScheduleCallback();
            return(sqlWorkflowInstanceStoreAsyncResult);
        }
예제 #10
0
        // The persistence engine will send a variety of commands to the configured InstanceStore,
        // such as CreateWorkflowOwnerCommand, SaveWorkflowCommand, and LoadWorkflowCommand.
        // This method is where we will handle those commands.
        protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
        {
            IDictionary <XName, InstanceValue> instanceStateData = null;

            //The CreateWorkflowOwner command instructs the instance store to create a new instance owner bound to the instanace handle
            if (command is CreateWorkflowOwnerCommand)
            {
                context.BindInstanceOwner(_storeId, Guid.NewGuid());
            }
            //The SaveWorkflow command instructs the instance store to modify the instance bound to the instance handle or an instance key
            else if (command is SaveWorkflowCommand)
            {
                SaveWorkflowCommand saveCommand = (SaveWorkflowCommand)command;
                instanceStateData = saveCommand.InstanceData;

                var instanceStateXml = DictionaryToXml(instanceStateData);
                Save(context.InstanceView.InstanceId, this._storeId, instanceStateXml);
            }
            //The LoadWorkflow command instructs the instance store to lock and load the instance bound to the identifier in the instance handle
            else if (command is LoadWorkflowCommand)
            {
                var xml = Load(context.InstanceView.InstanceId, this._storeId);
                instanceStateData = XmlToDictionary(xml);
                //load the data into the persistence Context
                context.LoadedInstance(InstanceState.Initialized, instanceStateData, null, null, null);
            }

            return(new CompletedAsyncResult <bool>(true, callback, state));
        }
예제 #11
0
 public SaveWorkflowAsyncResult(InstancePersistenceContext context, InstancePersistenceCommand command, SqlWorkflowInstanceStore store, SqlWorkflowInstanceStoreLock storeLock, Transaction currentTransaction, TimeSpan timeout, AsyncCallback callback, object state) : base(context, command, store, storeLock, currentTransaction, timeout, callback, state)
 {
     if (((SaveWorkflowCommand)command).InstanceKeyMetadataChanges.Count > 0)
     {
         throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(System.Activities.DurableInstancing.SR.InstanceKeyMetadataChangesNotSupported));
     }
 }
예제 #12
0
 public TryLoadRunnableWorkflowAsyncResult(InstancePersistenceContext context, InstancePersistenceCommand command, SqlWorkflowInstanceStore store, SqlWorkflowInstanceStoreLock storeLock, Transaction currentTransaction, TimeSpan timeout, AsyncCallback callback, object state) : base(context, command, store, storeLock, currentTransaction, timeout, callback, state)
 {
     if (base.Store.WorkflowHostType == Guid.Empty)
     {
         throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(command.Name, System.Activities.DurableInstancing.SR.TryLoadRequiresWorkflowType, null));
     }
 }
예제 #13
0
        internal IAsyncResult BeginTryCommandInternal(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
        {
            SqlWorkflowInstanceStoreAsyncResult result = null;

            if (command is SaveWorkflowCommand)
            {
                result = new SaveWorkflowAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is TryLoadRunnableWorkflowCommand)
            {
                result = new TryLoadRunnableWorkflowAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is LoadWorkflowCommand)
            {
                result = new LoadWorkflowAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is LoadWorkflowByInstanceKeyCommand)
            {
                result = new LoadWorkflowByKeyAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is ExtendLockCommand)
            {
                result = new ExtendLockAsyncResult(null, command, this, this.storeLock, null, timeout, callback, state);
            }
            else if (command is DetectRunnableInstancesCommand)
            {
                result = new DetectRunnableInstancesAsyncResult(null, command, this, this.storeLock, null, timeout, callback, state);
            }
            else if (command is DetectActivatableWorkflowsCommand)
            {
                result = new DetectActivatableWorkflowsAsyncResult(null, command, this, this.storeLock, null, timeout, callback, state);
            }
            else if (command is RecoverInstanceLocksCommand)
            {
                result = new RecoverInstanceLocksAsyncResult(null, command, this, this.storeLock, null, timeout, callback, state);
            }
            else if (command is UnlockInstanceCommand)
            {
                result = new UnlockInstanceAsyncResult(null, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is CreateWorkflowOwnerCommand)
            {
                result = new CreateWorkflowOwnerAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is DeleteWorkflowOwnerCommand)
            {
                result = new DeleteWorkflowOwnerAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else if (command is QueryActivatableWorkflowsCommand)
            {
                result = new QueryActivatableWorkflowAsyncResult(context, command, this, this.storeLock, Transaction.Current, timeout, callback, state);
            }
            else
            {
                return(base.BeginTryCommand(context, command, timeout, callback, state));
            }
            result.ScheduleCallback();
            return(result);
        }
예제 #14
0
        private bool CreateWorkflowOwner(InstancePersistenceContext context, CreateWorkflowOwnerCommand command)
        {
            Guid instanceOwnerId = Guid.NewGuid();

            context.BindInstanceOwner(instanceOwnerId, instanceOwnerId);
            context.BindEvent(HasRunnableWorkflowEvent.Value);
            return(true);
        }
예제 #15
0
        private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command)
        {
            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            if (command.CompleteInstance)
            {
                context.CompletedInstance();
                if (!KeepInstanceDataAfterCompletion && ToDelete != null)
                {
                    //DeleteFiles(context.InstanceView.InstanceId);
                    ToDelete();
                }
            }
            else
            {
                Dictionary <string, InstanceValue> instanceData     = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData);
                Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command);

                try
                {
                    //serialize_dc(instanceData, instanceMetadata);
                    //string serializedCorrelation = WorkflowSerialization.Serialize<WorkflowCorrelation>(this.Correlation);
                    string serializedInstanceData     = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceData);
                    string serializedInstanceMetadata = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceMetadata);

                    if (ToPersist != null)
                    {
                        ToPersist(new Serialized()
                        {
                            SerializedInstanceData     = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceData),
                            SerializedInstanceMetadata = WorkflowSerialization.Serialize <Dictionary <string, InstanceValue> >(instanceMetadata)
                        });
                    }
                }
                catch (Exception exc)
                {
                    throw exc;
                }

                context.PersistedInstance(command.InstanceData);
                if (command.CompleteInstance)
                {
                    context.CompletedInstance();
                }

                if (command.UnlockInstance || command.CompleteInstance)
                {
                    context.InstanceHandle.Free();
                }
            }

            return(true);
        }
예제 #16
0
        private static void UpdateKeyData(InstancePersistenceContext context, SaveWorkflowCommand saveWorkflowCommand)
        {
            InstanceView instanceView = context.InstanceView;

            foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > pair in saveWorkflowCommand.InstanceKeysToAssociate)
            {
                if (!instanceView.InstanceKeys.ContainsKey(pair.Key))
                {
                    context.AssociatedInstanceKey(pair.Key);
                    if (pair.Value != null)
                    {
                        foreach (KeyValuePair <XName, InstanceValue> pair2 in pair.Value)
                        {
                            context.WroteInstanceKeyMetadataValue(pair.Key, pair2.Key, pair2.Value);
                        }
                    }
                }
            }
            foreach (Guid guid in saveWorkflowCommand.InstanceKeysToComplete)
            {
                InstanceKeyView view2;
                if (instanceView.InstanceKeys.TryGetValue(guid, out view2) && (view2.InstanceKeyState != InstanceKeyState.Completed))
                {
                    context.CompletedInstanceKey(guid);
                }
            }
            foreach (Guid guid2 in saveWorkflowCommand.InstanceKeysToFree)
            {
                InstanceKeyView view3;
                if (instanceView.InstanceKeys.TryGetValue(guid2, out view3))
                {
                    context.UnassociatedInstanceKey(guid2);
                }
            }
            foreach (KeyValuePair <Guid, IDictionary <XName, InstanceValue> > pair3 in saveWorkflowCommand.InstanceKeyMetadataChanges)
            {
                if (pair3.Value != null)
                {
                    foreach (KeyValuePair <XName, InstanceValue> pair4 in pair3.Value)
                    {
                        context.WroteInstanceKeyMetadataValue(pair3.Key, pair4.Key, pair4.Value);
                    }
                }
            }
            if (saveWorkflowCommand.CompleteInstance)
            {
                foreach (KeyValuePair <Guid, InstanceKeyView> pair5 in instanceView.InstanceKeys)
                {
                    if ((pair5.Value != null) && (pair5.Value.InstanceKeyState == InstanceKeyState.Associated))
                    {
                        context.CompletedInstanceKey(pair5.Key);
                    }
                }
            }
        }
        protected override bool TryCommand(
            InstancePersistenceContext context,
            InstancePersistenceCommand command, TimeSpan timeout)
        {
            ManualResetEvent waitEvent   = new ManualResetEvent(false);
            IAsyncResult     asyncResult = BeginTryCommand(
                context, command, timeout, ar => waitEvent.Set(), null);

            waitEvent.WaitOne(timeout);
            return(EndTryCommand(asyncResult));
        }
예제 #18
0
        /// <summary>
        /// Handles a <see cref="CreateWorkflowOwnerCommand"/>.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="command"></param>
        Task <bool> CreateWorkflowOwnerCommand(InstancePersistenceContext context, CreateWorkflowOwnerCommand command)
        {
            if (state.InstanceOwnerId == Guid.Empty)
            {
                throw new InvalidOperationException("InstanceOwnerId is empty.");
            }

            context.BindInstanceOwner(state.InstanceOwnerId, Guid.NewGuid());

            return(Task.FromResult(true));
        }
예제 #19
0
 protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
 {
     try
     {
         return(base.BeginTryCommand(context, command, timeout, callback, state));
     }
     catch (Exception e)
     {
         return(new TypedCompletedAsyncResult <Exception>(e, callback, state));
     }
 }
예제 #20
0
        private static Owner CheckOwner(InstancePersistenceContext context, XName name)
        {
            Owner owner = PersistenceItemManager.Load <Owner>(context.InstanceView.InstanceOwner.InstanceOwnerId);

            if (owner == null || owner.LockToken != context.LockToken)
            {
                context.InstanceHandle.Free();
                throw new InstanceOwnerException(name, context.InstanceView.InstanceOwner.InstanceOwnerId);
            }

            return(owner);
        }
        protected override IAsyncResult BeginTryCommand(
            InstancePersistenceContext context,
            InstancePersistenceCommand command,
            TimeSpan timeout, AsyncCallback callback, object state)
        {
            switch (command.GetType().Name)
            {
            case "CreateWorkflowOwnerCommand":

                Func <Exception> createFunc = () => ProcessCreateWorkflowOwner(context,
                                                                               command as CreateWorkflowOwnerCommand);

                return(createFunc.BeginInvoke(ar =>
                {
                    Exception ex = createFunc.EndInvoke(ar);
                    callback(new InstanceStoreAsyncResult(ar, ex));
                }, state));

            case "LoadWorkflowCommand":
                Func <Exception> loadFunc = () => ProcessLoadWorkflow(context,
                                                                      command as LoadWorkflowCommand);

                return(loadFunc.BeginInvoke(ar =>
                {
                    Exception ex = loadFunc.EndInvoke(ar);
                    callback(new InstanceStoreAsyncResult(ar, ex));
                }, state));

            case "LoadWorkflowByInstanceKeyCommand":
                Func <Exception> loadByKeyFunc = () => ProcessLoadWorkflowByInstanceKey(context,
                                                                                        command as LoadWorkflowByInstanceKeyCommand);

                return(loadByKeyFunc.BeginInvoke(ar =>
                {
                    Exception ex = loadByKeyFunc.EndInvoke(ar);
                    callback(new InstanceStoreAsyncResult(ar, ex));
                }, state));

            case "SaveWorkflowCommand":
                Func <Exception> saveFunc = () => ProcessSaveWorkflow(context,
                                                                      command as SaveWorkflowCommand);

                return(saveFunc.BeginInvoke(ar =>
                {
                    Exception ex = saveFunc.EndInvoke(ar);
                    callback(new InstanceStoreAsyncResult(ar, ex));
                }, state));

            default:
                return(base.BeginTryCommand(
                           context, command, timeout, callback, state));
            }
        }
예제 #22
0
        public LoadRetryAsyncResult(SqlWorkflowInstanceStore store, InstancePersistenceContext context,
                                    InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
            : base(callback, state)
        {
            this.InstanceStore = store;
            this.InstancePersistenceContext = context;
            this.InstancePersistenceCommand = command;
            this.commandTimeout             = new TimeoutHelper(timeout);

            InstanceStore.BeginTryCommandInternal(this.InstancePersistenceContext, this.InstancePersistenceCommand,
                                                  this.commandTimeout.RemainingTime(), LoadRetryAsyncResult.onTryCommandCallback, this);
        }
예제 #23
0
        private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command)
        {
            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            if (command.CompleteInstance)
            {
                context.CompletedInstance();
                if (!KeepInstanceDataAfterCompletion)
                {
                    DeleteFiles(context.InstanceView.InstanceId);
                }
            }
            else
            {
                Dictionary <string, InstanceValue> instanceData     = SerializeablePropertyBagConvertXNameInstanceValue(command.InstanceData);
                Dictionary <string, InstanceValue> instanceMetadata = SerializeInstanceMetadataConvertXNameInstanceValue(context, command);

                try
                {
                    var serializedInstanceData = JsonConvert.SerializeObject(instanceData, Formatting.Indented, _jsonSerializerSettings);
                    File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceData", serializedInstanceData);

                    var serializedInstanceMetadata = JsonConvert.SerializeObject(instanceMetadata, Formatting.Indented, _jsonSerializerSettings);
                    File.WriteAllText(_storeDirectoryPath + "\\" + context.InstanceView.InstanceId + "-InstanceMetadata", serializedInstanceMetadata);
                }
                catch (Exception)
                {
                    throw;
                }

                foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges)
                {
                    context.WroteInstanceMetadataValue(property.Key, property.Value);
                }

                context.PersistedInstance(command.InstanceData);
                if (command.CompleteInstance)
                {
                    context.CompletedInstance();
                }

                if (command.UnlockInstance || command.CompleteInstance)
                {
                    context.InstanceHandle.Free();
                }
            }

            return(true);
        }
예제 #24
0
        // The persistence engine will send a variety of commands to the configured InstanceStore,
        // such as CreateWorkflowOwnerCommand, SaveWorkflowCommand, and LoadWorkflowCommand.
        // This method is where we will handle those commands.
        protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
        {
            IDictionary <XName, InstanceValue> instanceStateData = null;
            WorkflowInstance instance   = null;
            Guid             InstanceId = Guid.Empty;

            if (context != null && context.InstanceView != null)
            {
                InstanceId = context.InstanceView.InstanceId;
                instance   = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
            }

            //The CreateWorkflowOwner command instructs the instance store to create a new instance owner bound to the instanace handle
            if (command is CreateWorkflowOwnerCommand)
            {
                context.BindInstanceOwner(_storeId, Guid.NewGuid());
            }
            //The SaveWorkflow command instructs the instance store to modify the instance bound to the instance handle or an instance key
            else if (command is SaveWorkflowCommand)
            {
                if (instance != null && instance.Workflow != null && instance.Workflow.Serializable == true)
                {
                    SaveWorkflowCommand saveCommand = (SaveWorkflowCommand)command;
                    try
                    {
                        instanceStateData = saveCommand.InstanceData;
                        var instanceStateXml = DictionaryToXml(instanceStateData);
                        Save(context.InstanceView.InstanceId, this._storeId, instanceStateXml);
                    }
                    catch (Exception)
                    {
                        instance.Workflow.Serializable = false;
                    }
                }
            }
            //The LoadWorkflow command instructs the instance store to lock and load the instance bound to the identifier in the instance handle
            else if (command is LoadWorkflowCommand)
            {
                var xml = Load(InstanceId, _storeId);
                // if (xml == null) throw new ArgumentNullException("Failed locating instance data for " + context.InstanceView.InstanceId.ToString());
                // if (xml == null) return new CompletedAsyncResult<bool>(false, callback, state);
                if (xml == null)
                {
                    return(new CompletedAsyncResult <bool>(true, callback, state));
                }
                instanceStateData = XmlToDictionary(xml);
                //load the data into the persistence Context
                context.LoadedInstance(InstanceState.Initialized, instanceStateData, null, null, null);
            }
            return(new CompletedAsyncResult <bool>(true, callback, state));
        }
 protected SqlWorkflowInstanceStoreAsyncResult
 (
     InstancePersistenceContext context,
     InstancePersistenceCommand command,
     SqlWorkflowInstanceStore store,
     SqlWorkflowInstanceStoreLock storeLock,
     Transaction currentTransaction,
     TimeSpan timeout,
     AsyncCallback callback,
     object state
 )
     : this(context, command, store, storeLock, currentTransaction, timeout, store.MaxConnectionRetries, callback, state)
 {
 }
예제 #26
0
 public LoadWorkflowByKeyAsyncResult
 (
     InstancePersistenceContext context,
     InstancePersistenceCommand command,
     SqlWorkflowInstanceStore store,
     SqlWorkflowInstanceStoreLock storeLock,
     Transaction currentTransaction,
     TimeSpan timeout,
     AsyncCallback callback,
     object state
 ) :
     base(context, command, store, storeLock, currentTransaction, timeout, callback, state)
 {
 }
예제 #27
0
        private bool LoadWorkflow(InstancePersistenceContext context, LoadWorkflowCommand command)
        {
            if (command.AcceptUninitializedInstance)
            {
                return(false);
            }

            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            IDictionary <XName, InstanceValue> instanceData     = null;
            IDictionary <XName, InstanceValue> instanceMetadata = null;

            FileStream instanceDataStream;
            FileStream instanceMetadataStream;

            GetFileStreams(context.InstanceView.InstanceId, out instanceDataStream, out instanceMetadataStream, FileMode.Open);

            DataContractSerializer instanceDataSerializer;
            DataContractSerializer instanceMetadataSerializer;

            GetDataContractSerializers(out instanceDataSerializer, out instanceMetadataSerializer);

            Dictionary <string, InstanceValue> serializableInstanceData;
            Dictionary <string, InstanceValue> serializableInstanceMetadata;

            try
            {
                serializableInstanceData     = (Dictionary <string, InstanceValue>)instanceDataSerializer.ReadObject(instanceDataStream);
                serializableInstanceMetadata = (Dictionary <string, InstanceValue>)instanceMetadataSerializer.ReadObject(instanceMetadataStream);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                instanceDataStream.Dispose();
                instanceMetadataStream.Dispose();
            }

            instanceData     = this.DeserializePropertyBagConvertXNameInstanceValue(serializableInstanceData);
            instanceMetadata = this.DeserializePropertyBagConvertXNameInstanceValue(serializableInstanceMetadata);

            context.LoadedInstance(InstanceState.Initialized, instanceData, instanceMetadata, null, null);

            return(true);
        }
 public TestDatabaseVersionAndRunAsyncResult(
     InstancePersistenceContext context,
     InstancePersistenceCommand command,
     SqlWorkflowInstanceStore store,
     SqlWorkflowInstanceStoreLock storeLock,
     Transaction currentTransaction,
     TimeSpan timeout,
     Version targetVersion,
     AsyncCallback callback,
     object state) :
     base(context, command, store, storeLock, currentTransaction, timeout, callback, state)
 {
     this.currentTransaction = currentTransaction;
     this.targetVersion      = targetVersion;
 }
예제 #29
0
        private bool LoadWorkflow(InstancePersistenceContext context, LoadWorkflowCommand command)
        {
            if (command.AcceptUninitializedInstance)
            {
                return(false);
            }

            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            IDictionary <XName, InstanceValue> instanceData     = null;
            IDictionary <XName, InstanceValue> instanceMetadata = null;

            Dictionary <string, InstanceValue> serializableInstanceData     = null;
            Dictionary <string, InstanceValue> serializableInstanceMetadata = null;

            try
            {
                if (ToLoad != null)
                {
                    Serialized serialized = ToLoad();

                    serializableInstanceData     = WorkflowSerialization.DeSerialize <Dictionary <string, InstanceValue> >(serialized.SerializedInstanceData);
                    serializableInstanceMetadata = WorkflowSerialization.DeSerialize <Dictionary <string, InstanceValue> >(serialized.SerializedInstanceMetadata);
                }
            }
            catch (Exception exc)
            {
                throw;
            }

            if (serializableInstanceData != null)
            {
                instanceData = this.DeserializePropertyBagConvertXNameInstanceValue(serializableInstanceData);
            }

            if (serializableInstanceMetadata != null)
            {
                instanceMetadata = this.DeserializePropertyBagConvertXNameInstanceValue(serializableInstanceMetadata);
            }

            context.LoadedInstance(InstanceState.Initialized, instanceData, instanceMetadata, null, null);

            return(true);
        }
예제 #30
0
        private bool SaveWorkflow(InstancePersistenceContext context, SaveWorkflowCommand command)
        {
            if (context.InstanceVersion == -1)
            {
                context.BindAcquiredLock(0);
            }

            if (command.CompleteInstance)
            {
                context.CompletedInstance();
            }
            else
            {
                string instanceType = "";

                const string  InstanceTypeXName = "{urn:schemas-microsoft-com:System.Runtime.DurableInstancing/4.0/metadata}InstanceType";
                InstanceValue instanceTypeInstanceValue;
                if (command.InstanceMetadataChanges.TryGetValue(InstanceTypeXName, out instanceTypeInstanceValue))
                {
                    instanceType = instanceTypeInstanceValue.Value.ToString();
                }

                Dictionary <string, object> fullInstanceData = new Dictionary <string, object>();
                fullInstanceData.Add("instanceId", context.InstanceView.InstanceId);
                fullInstanceData.Add("instanceOwnerId", context.InstanceView.InstanceOwner.InstanceOwnerId);
                fullInstanceData.Add("instanceData", SerializeablePropertyBag(command.InstanceData));
                fullInstanceData.Add("instanceMetadata", SerializeInstanceMetadata(context, command));

                foreach (KeyValuePair <XName, InstanceValue> property in command.InstanceMetadataChanges)
                {
                    context.WroteInstanceMetadataValue(property.Key, property.Value);
                }

                context.PersistedInstance(command.InstanceData);

                _stores.Save(WorkflowStoreComponents.Definition
                             | WorkflowStoreComponents.Metadata
                             | WorkflowStoreComponents.Streams
                             | WorkflowStoreComponents.TerminatingError
                             | WorkflowStoreComponents.Timer
                             | WorkflowStoreComponents.ActivityState
                             | WorkflowStoreComponents.JobState,
                             fullInstanceData);
            }

            return(true);
        }