// 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)); }
private bool CreateWorkflowOwner(InstancePersistenceContext context, CreateWorkflowOwnerCommand command) { Guid instanceOwnerId = Guid.NewGuid(); context.BindInstanceOwner(instanceOwnerId, instanceOwnerId); context.BindEvent(HasRunnableWorkflowEvent.Value); return(true); }
/// <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)); }
// 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)); }
private Exception ProcessCreateWorkflowOwner( InstancePersistenceContext context, CreateWorkflowOwnerCommand command) { try { context.BindInstanceOwner(_ownerId, _lockToken); return(null); } catch (InstancePersistenceException exception) { Console.WriteLine( "ProcessCreateWorkflowOwner exception: {0}", exception.Message); return(exception); } }
//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> data = null; //The CreateWorkflowOwner command instructs the instance store to create a new instance owner bound to the instanace handle if (command is CreateWorkflowOwnerCommand || command is CreateWorkflowOwnerWithIdentityCommand) //Arghya test { context.BindInstanceOwner(ownerInstanceID, Guid.NewGuid()); //TODO?? } //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; data = saveCommand.InstanceData; Save(data, context.InstanceView.InstanceId.ToString()); //arghya } //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) { //string fileName = Path.Combine(Directory.GetCurrentDirectory(), string.Format(CultureInfo.InvariantCulture, InstanceFormatString, this.ownerInstanceID)); string fileName = Path.Combine( Directory.GetCurrentDirectory(), string.Format(CultureInfo.InvariantCulture, InstanceFormatString, context.InstanceView.InstanceId.ToString())); //this.ownerInstanceID //arghya try { using (FileStream inputStream = new FileStream(fileName, FileMode.Open)) { data = LoadInstanceDataFromFile(inputStream); //load the data into the persistence Context context.LoadedInstance(InstanceState.Initialized, data, null, null, null); } } catch (Exception exception) { throw new FileLoadException(exception.Message); //PersistenceException //using System.ServiceModel.Persistence; } } return(new CompletedAsyncResult <bool>(true, callback, state)); }
protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state) { if (command is CreateWorkflowOwnerCommand) { context.BindInstanceOwner(OwnerInstanceId, Guid.NewGuid()); } else if (command is SaveWorkflowCommand) { var saveCommand = (SaveWorkflowCommand)command; Save(context.InstanceView.InstanceId, saveCommand.InstanceData); } else if (command is LoadWorkflowCommand) { context.LoadedInstance(InstanceState.Initialized, Load(context.InstanceView.InstanceId), null, null, null); } else if (command is DeleteWorkflowOwnerCommand) { } return(new CompletedAsyncResult <bool>(true, callback, state)); }
private Exception ProcessCreateWorkflowOwner( InstancePersistenceContext context, // ReSharper disable UnusedParameter.Local CreateWorkflowOwnerCommand command) // ReSharper restore UnusedParameter.Local { try { context.BindInstanceOwner(_ownerId, _lockToken); return(null); } catch (InstancePersistenceException exception) { Console.WriteLine( // ReSharper disable LocalizableElement "ProcessCreateWorkflowOwner exception: {0}", // ReSharper restore LocalizableElement exception.Message); return(exception); } }
/// <summary> /// The begin try command. /// </summary> /// <param name="context">The context.</param> /// <param name="command">The command.</param> /// <param name="timeout">The timeout.</param> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns>The <see cref="IAsyncResult" />.</returns> /// <exception cref="SystemFailureException">Error processing message.</exception> protected override IAsyncResult BeginTryCommand( InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state) { lock (LocalLock) { if (command is CreateWorkflowOwnerCommand) { context.BindInstanceOwner(this.ownerInstanceId, Guid.NewGuid()); } else { var saveWorkflowCommand = command as SaveWorkflowCommand; if (saveWorkflowCommand != null) { this.SaveInstanceData(saveWorkflowCommand.InstanceData); } else if (command is LoadWorkflowCommand) { try { var instanceData = this.unstructuredStorageRepository.GetById( "WorkflowInstanceStoreData", this.ownerInstanceId.ToString()); var deserializedData = DeserializeData(instanceData.Payload.Combine()); context.LoadedInstance(InstanceState.Initialized, deserializedData, null, null, null); } catch (Exception exception) { throw; } } } return(new CompletedAsyncResult <bool>(true, callback, state)); } }
//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> data = 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(ownerInstanceID, 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; data = saveCommand.InstanceData; Save(data); } //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) { string fileName = this.ownerInstanceID.ToString(); try { using (FileStream inputStream = new FileStream(fileName, FileMode.Open)) { data = LoadInstanceDataFromFile(inputStream); //load the data into the persistence Context context.LoadedInstance(InstanceState.Initialized, data, null, null, null); } } catch (Exception exception) { throw new Exception(exception.Message); } } return(new CompletedAsyncResult <bool>(true, callback, state)); }
//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> data = 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(ownerInstanceID, ownerInstanceID); } //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; data = saveCommand.InstanceData; Save(data); //_saveWaitHandler.WaitOne(new TimeSpan(0, 0, 1)); } //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) { string fileName = GetFileName(this.ownerInstanceID); data = LoadInstanceDataFromFile(fileName); var nonWriteOnly = data.Where(kvp => (kvp.Value.Options & InstanceValueOptions.WriteOnly) != InstanceValueOptions.WriteOnly).ToDictionary(k => k.Key, v => v.Value); //load the data into the persistence Context context.LoadedInstance(InstanceState.Initialized, nonWriteOnly, null, null, null); } else if (command.Name.LocalName == "CreateWorkflowOwner") { //do nothing } var result = new CompletedAsyncResult <bool>(true, callback, state); return(result); }
protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state) { SaveWorkflowCommand save = command as SaveWorkflowCommand; if (save != null) { lock (FileStore.s_thisLock) { ProcessSaveCommand(context, save); } } LoadWorkflowCommand load = command as LoadWorkflowCommand; if (load != null) { lock (FileStore.s_thisLock) { ProcessLoadCommand(context, load); } } LoadWorkflowByInstanceKeyCommand loadByKey = command as LoadWorkflowByInstanceKeyCommand; if (loadByKey != null) { lock (FileStore.s_thisLock) { ProcessLoadByKeyCommand(context, loadByKey); } } if (save != null || load != null || loadByKey != null) { return(new CompletedAsyncResult(callback, state)); } if (command is CreateWorkflowOwnerCommand createOwner) { Guid ownerId = Guid.NewGuid(); Owner owner = new Owner(); lock (s_thisLock) { owner.Id = ownerId; owner.LockToken = Guid.NewGuid(); owner.Metadata = new PropertyBag(createOwner.InstanceOwnerMetadata); PersistenceItemManager.SaveToFile <Owner>(owner); } context.BindInstanceOwner(ownerId, owner.LockToken); context.BindEvent(HasRunnableWorkflowEvent.Value); return(new CompletedAsyncResult(callback, state)); } if (command is DeleteWorkflowOwnerCommand deleteOwner) { Guid ownerId = context.InstanceView.InstanceOwner.InstanceOwnerId; lock (FileStore.s_thisLock) { Owner owner = PersistenceItemManager.Load <Owner>(ownerId); if (owner != null && owner.LockToken == context.LockToken) { PersistenceItemManager.Remove <Owner>(ownerId); } } context.InstanceHandle.Free(); return(new CompletedAsyncResult(callback, state)); } return(base.BeginTryCommand(context, command, timeout, callback, state)); }