Dev2.DynamicServices.FileSystemInstanceStoreIO.SaveAllInstanceData C# (CSharp) Method

SaveAllInstanceData() public method

Saves all instance data.
public SaveAllInstanceData ( System.Guid instanceId, System.Activities.DurableInstancing.SaveWorkflowCommand command ) : System.Boolean
instanceId System.Guid The instance id.
command System.Activities.DurableInstancing.SaveWorkflowCommand The command.
return System.Boolean
        public Boolean SaveAllInstanceData(Guid instanceId, SaveWorkflowCommand command)
        {
            Boolean isExistingInstance;
            try
            {
                String fileName = String.Format("{0}.xml", instanceId);
                String fullPath = Path.Combine(_dataDirectory, fileName);
                isExistingInstance = File.Exists(fullPath);

                XElement root = new XElement("Instance");
                root.Add(new XAttribute("WorkflowInstanceId", instanceId));
                XDocument xml = new XDocument(root);

                NetDataContractSerializer serializer = new NetDataContractSerializer();

                XElement section = new XElement("InstanceData");
                root.Add(section);
                foreach(var entry in command.InstanceData)
                {
                    SaveSingleEntry(serializer, section, entry);
                }
                SaveInstanceDocument(fullPath, xml);
            }
            catch(Exception exception)
            {
                Dev2Logger.Log.Error(exception);
                throw new InstancePersistenceException(exception.Message, exception);
            }
            return isExistingInstance;
        }

Usage Example

        // ReSharper disable InconsistentNaming - Unit Tests
        public void SaveAllInstanceData_With_FileUsedByAnotherProcess_Expected_ThrowsInstancePersistenceException()
        // ReSharper restore InconsistentNaming
        {
            var instanceId = Guid.NewGuid();
            var path = Path.Combine(TestPath, instanceId + ".xml");

            try
            {
                var xml = XmlResource.Fetch(TestFileName);
                xml.Save(path);

                // Force error: The process cannot access the file because it is being used by another process
                using(var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    var store = new FileSystemInstanceStoreIO();
                    var result = store.SaveAllInstanceData(instanceId, new SaveWorkflowCommand());
                }
            }
            finally
            {
                if(File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }