//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; //The CreateWorkflowOwner command instructs the instance store to create a new instance owner bound to the instanace handle if (command is CreateWorkflowOwnerCommand) { context.BindInstanceOwner(this.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) { var saveCommand = (SaveWorkflowCommand)command; data = saveCommand.InstanceData; this.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) { var fileName = IoHelper.GetFileName(this.ownerInstanceId); using (var inputStream = new FileStream(fileName, FileMode.Open)) { data = LoadInstanceDataFromFile(inputStream); //load the data into the persistence Context context.LoadedInstance(InstanceState.Initialized, data, 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> /// 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); } }
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; } }