Ejemplo n.º 1
0
        public static StorageMetadata ReadMetadata(ILogger logger)
        {
            StorageMetadata result = null;

            if (File.Exists("metadata.bin"))
            {
                try
                {
                    int i = 5;
                    do
                    {
                        try
                        {
                            using (Stream s = File.Open("metadata.bin", FileMode.Open, FileAccess.ReadWrite))
                            {
                                BinaryFormatter bf = new BinaryFormatter();
                                result = bf.Deserialize(s) as StorageMetadata;
                            }
                            break;
                        }
                        catch (IOException)
                        {
                            if (--i <= 0)
                            {
                                throw;
                            }

                            logger.Log("StorageMetadata", "Reading metadata exception! Remaining atteprts: " + i,
                                       MessageLevel.Warning);
                            Thread.Sleep(new TimeSpan(0, 0, 0, (5 - i) * 2));
                        }
                    }while (i > 0);
                    logger.Log("StorageMetadata", "StorageMetadata loaded successfully", MessageLevel.Info);
                    return(result);
                }
                catch (SerializationException)
                {
                    logger.Log("StorageMetadata", "StorageMetadata not loaded. Creating new.", MessageLevel.Warning);
                    return(new StorageMetadata()
                    {
                        Databases = new ConcurrentDictionary <Did, IDatabaseParameters>()
                    });
                }
            }
            else
            {
                logger.Log("StorageMetadata", "StorageMetadata not loaded. Creating new.", MessageLevel.Warning);
                return(new StorageMetadata()
                {
                    Databases = new ConcurrentDictionary <Did, IDatabaseParameters>()
                });
            }
        }
Ejemplo n.º 2
0
 public static void SaveMetadata(StorageMetadata metadata, ILogger logger)
 {
     try
     {
         using (Stream s = File.Open("metadata.bin", FileMode.Create, FileAccess.ReadWrite))
         {
             BinaryFormatter bf = new BinaryFormatter();
             bf.Serialize(s, metadata);
         }
         logger.Log("StorageMetadata", "StorageMetadata saved successfully", MessageLevel.Info);
     }
     catch (SerializationException ex)
     {
         logger.Log("StorageMetadata", string.Format("StorageMetadata not saved.{0}", ex), MessageLevel.Error);
     }
 }