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

SaveAllInstanceMetaData() public method

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

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

                NetDataContractSerializer serializer =
                    new NetDataContractSerializer();

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

Usage Example

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

            try
            {
                var xml = XmlResource.Fetch(TestMetaFileName);
                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();
                    store.SaveAllInstanceMetaData(instanceId, new SaveWorkflowCommand());
                }
            }
            finally
            {
                if(File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }