Esempio n. 1
0
        public async Task<AppConfiguration> LoadConfigData()
        {
            try
            {
                var folder = await CreateFolderToSaveAsync();
                if (folder == null) return null;

                var fileExists = await folder.CheckExistsAsync(FileName);
                if (fileExists == ExistenceCheckResult.FileExists)
                {
                    var file = await folder.GetFileAsync(FileName);
                    if (file == null) return null;

                    var jsonData = await file.ReadAllTextAsync();
                    if (string.IsNullOrEmpty(jsonData)) return null;

                    var dic = JsonConvert.DeserializeObject<Dictionary<string, AppConfigurationValue>>(jsonData);

                    var appConfig = new AppConfiguration();
                    foreach (var value in dic.Values)
                    {
                        if (string.IsNullOrEmpty(value?.Attributes?.Name)) continue;
                        appConfig[value.Attributes.Name] = value;
                    }

                    return appConfig;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return null;
        }
Esempio n. 2
0
        public async Task<bool> Save(AppConfiguration appConfiguration, TimeSpan expirationTime = default(TimeSpan))
        {
            var res = false;

            if (appConfiguration == null) return res;

            try
            {
                var folder = await CreateFolderToSaveAsync();
                if (folder == null) return res;

                var file = await folder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
                if (file == null) return res;

                var appConfigJson = JsonConvert.SerializeObject(appConfiguration);
                await file.WriteAllTextAsync(appConfigJson);

                SetCacheExpirationTime(expirationTime);

                res = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return res;
        }
Esempio n. 3
0
        public async Task<bool> Save(AppConfiguration appConfiguration, TimeSpan expirationTime = default(TimeSpan))
        {
            var res = false;

            if (appConfiguration == null) return res;

            try
            {
                var folder = await CreateFolderToSaveAsync();
                if (folder == null) return res;

                var file = await folder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
                if (file == null) return res;

                var valuesDict = new Dictionary<string,AppConfigurationValue>();

                var keys = appConfiguration.Keys;
                foreach (var key in keys)
                {
                    if (!valuesDict.ContainsKey(key) && appConfiguration[key] != null)
                    {
                        valuesDict.Add(key, appConfiguration[key]);
                    }
                }

                var appConfigJson = JsonConvert.SerializeObject(valuesDict, Formatting.Indented);
                await file.WriteAllTextAsync(appConfigJson);

                SetCacheExpirationTime(expirationTime);

                res = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return res;
        }
        private AppConfiguration CreateAppConfigurationOutOfServerResponse(AppConfigurationServerResponse appConfigurationServerResponse)
        {
            AppConfiguration res = null;
            if ((appConfigurationServerResponse?.HasErrors ?? true) || !appConfigurationServerResponse.HasConfigValues) return res;

            res = new AppConfiguration();
            foreach (var value in appConfigurationServerResponse.AppConfigValues)
            {
                if (string.IsNullOrEmpty(value?.Attributes?.Name)) continue;

                res[value.Attributes.Name] = value;
            }

            return res;
        }