//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 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; }
private void SharedLoadWorkflow(InstancePersistenceContext context, Guid instanceId) { if(instanceId != Guid.Empty) { IDictionary<XName, InstanceValue> instanceData; IDictionary<XName, InstanceValue> instanceMetadata; _dataStore.LoadInstance(instanceId, out instanceData, out instanceMetadata); if(context.InstanceView.InstanceId == Guid.Empty) { context.BindInstance(instanceId); } context.LoadedInstance(InstanceState.Initialized, instanceData, instanceMetadata, null, null); } else { throw new InstanceNotReadyException( String.Format("Unable to load instance: {0}", instanceId)); } }
/// <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 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; } }