Beispiel #1
0
        protected async Task <LaunchSettingsData> ReadSettingsFileFromDiskAsync()
        {
            string fileName = await GetLaunchSettingsFilePathAsync();

            string jsonString = _fileSystem.ReadAllText(fileName);

            // Since the sections in the settings file are extensible we iterate through each one and have the appropriate provider
            // serialize their section. Unfortunately, this means the data is string to object which is messy to deal with
            var launchSettingsData = new LaunchSettingsData()
            {
                OtherSettings = new Dictionary <string, object>(StringComparer.Ordinal)
            };
            var jsonObject = JObject.Parse(jsonString);

            foreach ((string key, JToken jToken) in jsonObject)
            {
                if (key.Equals(ProfilesSectionName, StringComparison.Ordinal) && jToken is JObject jObject)
                {
                    Dictionary <string, LaunchProfileData> profiles = LaunchProfileData.DeserializeProfiles(jObject);
                    launchSettingsData.Profiles = FixupProfilesAndLogErrors(profiles);
                }
                else
                {
                    // Find the matching json serialization handler for this section
                    Lazy <ILaunchSettingsSerializationProvider, IJsonSection> handler = JsonSerializationProviders.FirstOrDefault(sp => string.Equals(sp.Metadata.JsonSection, key));
                    if (handler != null)
                    {
                        object sectionObject = JsonConvert.DeserializeObject(jToken.ToString(), handler.Metadata.SerializationType);
                        launchSettingsData.OtherSettings.Add(key, sectionObject);
                    }
                    else
                    {
                        // We still need to remember settings for which we don't have an extensibility component installed. For this we
                        // just keep the jObject which can be serialized back out when the file is written.
                        launchSettingsData.OtherSettings.Add(key, jToken);
                    }
                }
            }

            // Remember the time we are sync'd to
            LastSettingsFileSyncTime = _fileSystem.LastFileWriteTime(fileName);
            return(launchSettingsData);
        }
Beispiel #2
0
        /// <summary>
        /// Reads the data from the launch settings file and returns it in a dictionary of settings section to object. Adds n error list entries
        /// and throws if an exception occurs
        /// </summary>
        protected LaunchSettingsData ReadSettingsFileFromDisk()
        {
            // Clear errors
            ClearErrors();
            try
            {
                string jsonString = FileManager.ReadAllText(LaunchSettingsFile);

                // Since the sections in the settings file are extensible we iterate through each one and have the appropriate provider
                // serialize their section. Unfortunately, this means the data is string to object which is messy to deal with
                var launchSettingsData = new LaunchSettingsData()
                {
                    OtherSettings = new Dictionary <string, object>(StringComparer.Ordinal)
                };
                JObject jsonObject = JObject.Parse(jsonString);
                foreach (var pair in jsonObject)
                {
                    if (pair.Key.Equals(ProfilesSectionName, StringComparison.Ordinal) && pair.Value is JObject)
                    {
                        var profiles = LaunchProfileData.DeserializeProfiles((JObject)pair.Value);
                        launchSettingsData.Profiles = FixupProfilesAndLogErrors(profiles);
                    }
                    else
                    {
                        // Find the matching json serialization handler for this section
                        var handler = JsonSerializationProviders.FirstOrDefault(sp => string.Equals(sp.Metadata.JsonSection, pair.Key));
                        if (handler != null)
                        {
                            object sectionObject = JsonConvert.DeserializeObject(pair.Value.ToString(), handler.Metadata.SerializationType);
                            launchSettingsData.OtherSettings.Add(pair.Key, sectionObject);
                        }
                        else
                        {
                            // We still need to remember settings for which we don't have an extensibility component installed. For this we
                            // just keep the jObject which can be serialized back out when the file is written.
                            launchSettingsData.OtherSettings.Add(pair.Key, pair.Value);
                        }
                    }
                }

                // Remember the time we are sync'd to
                LastSettingsFileSyncTime = FileManager.LastFileWriteTime(LaunchSettingsFile);
                return(launchSettingsData);
            }
            catch (JsonReaderException readerEx)
            {
                string err = string.Format(Resources.JsonErrorReadingLaunchSettings, readerEx.Message);
                LogError(err, LaunchSettingsFile, readerEx.LineNumber, readerEx.LinePosition, false);
                throw;
            }
            catch (JsonException jsonEx)
            {
                string err = string.Format(Resources.JsonErrorReadingLaunchSettings, jsonEx.Message);
                LogError(err, LaunchSettingsFile, -1, -1, false);
                throw;
            }
            catch (Exception ex)
            {
                string err = string.Format(Resources.ErrorReadingLaunchSettings, Path.Combine(LaunchSettingsFileFolder, LaunchSettingsFilename), ex.Message);
                LogError(err, false);
                throw;
            }
        }