Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationService" /> class.
        /// </summary>
        /// <param name="serializationManager">The serialization manager.</param>
        /// <param name="objectConverterService">The object converter service.</param>
        public ConfigurationService(ISerializationManager serializationManager, IObjectConverterService objectConverterService)
        {
            Argument.IsNotNull(() => serializationManager);
            Argument.IsNotNull(() => objectConverterService);

            _serializationManager   = serializationManager;
            _objectConverterService = objectConverterService;

#if NET
            _configFilePath = Path.Combine(Path.GetApplicationDataDirectory(), "configuration.xml");

            try
            {
                if (File.Exists(_configFilePath))
                {
                    using (var fileStream = new FileStream(_configFilePath, FileMode.Open))
                    {
                        _configuration = ModelBase.Load <DynamicConfiguration>(fileStream, SerializationMode.Xml);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load configuration, using default settings");
            }

            if (_configuration == null)
            {
                _configuration = new DynamicConfiguration();
            }
#endif
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationService" /> class.
        /// </summary>
        /// <param name="serializationManager">The serialization manager.</param>
        /// <param name="objectConverterService">The object converter service.</param>
        /// <param name="serializer">The serializer.</param>
        public ConfigurationService(ISerializationManager serializationManager,
                                    IObjectConverterService objectConverterService, ISerializer serializer)
        {
            Argument.IsNotNull("serializationManager", serializationManager);
            Argument.IsNotNull("objectConverterService", objectConverterService);
            Argument.IsNotNull("serializer", serializer);

            _serializationManager   = serializationManager;
            _objectConverterService = objectConverterService;
            _serializer             = serializer;

#if NET || NETSTANDARD
            _localConfigFilePath = Path.Combine(Path.GetApplicationDataDirectory(Catel.IO.ApplicationDataTarget.UserLocal), "configuration.xml");

            try
            {
                if (File.Exists(_localConfigFilePath))
                {
                    using (var fileStream = new FileStream(_localConfigFilePath, FileMode.Open))
                    {
                        _localConfiguration = SavableModelBase <DynamicConfiguration> .Load(fileStream, _serializer);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load local configuration, using default settings");
            }

            if (_localConfiguration == null)
            {
                _localConfiguration = new DynamicConfiguration();
            }

            _roamingConfigFilePath = Path.Combine(Path.GetApplicationDataDirectory(Catel.IO.ApplicationDataTarget.UserRoaming), "configuration.xml");

            try
            {
                if (File.Exists(_roamingConfigFilePath))
                {
                    using (var fileStream = new FileStream(_roamingConfigFilePath, FileMode.Open))
                    {
                        _roamingConfiguration = SavableModelBase <DynamicConfiguration> .Load(fileStream, _serializer);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to load roaming configuration, using default settings");
            }

            if (_roamingConfiguration == null)
            {
                _roamingConfiguration = new DynamicConfiguration();
            }
#endif
        }