コード例 #1
0
        public Dictionary <string, string> GetSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey)
        {
            var serviceContract = !String.IsNullOrEmpty(user) ?
                                  typeof(IConfigurationService) : typeof(IApplicationConfigurationReadService);
            var service = (IApplicationConfigurationReadService)Platform.GetService(serviceContract);

            using (service as IDisposable)
            {
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                var document    = GetConfigurationDocument(service, documentKey);

                var values = new Dictionary <string, string>();
                var parser = new SettingsParser();
                parser.FromXml(document, values);
                return(values);
            }
        }
コード例 #2
0
        public void PutSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey, Dictionary <string, string> dirtyValues)
        {
            // note: if user == null, we are saving shared settings, if user is valued, we are saving user settings
            // but both are never edited as a single operation

            // the approach taken here is to create an XML document that represents a diff between
            // the default settings (as specified by the settings group meta-data) and the modified settings,
            // and store that document in the configuration store

            var service = Platform.GetService <IConfigurationService>();

            using (service as IDisposable)
                using (var offlineCacheClient = _offlineCache.CreateClient())
                {
                    // first obtain the meta-data for the settings group properties
                    var properties = ListSettingsProperties(group, service);

                    // next we obtain any previously stored configuration document for this settings group
                    var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                    var document    = GetConfigurationDocument(service, documentKey);

                    // parse document
                    var parser = new SettingsParser();
                    var values = new Dictionary <string, string>();
                    parser.FromXml(document, values);

                    // update the values that have changed
                    foreach (var kvp in dirtyValues)
                    {
                        values[kvp.Key] = kvp.Value;
                    }

                    // now remove any values that are identical to the default values
                    foreach (var property in properties)
                    {
                        string value;
                        if (values.TryGetValue(property.Name, out value) && Equals(value, property.DefaultValue))
                        {
                            values.Remove(property.Name);
                        }
                    }

                    try
                    {
                        if (values.Count > 0)
                        {
                            // generate the document, update local cache and server
                            document = parser.ToXml(values);
                            offlineCacheClient.Put(documentKey, document);
                            service.SetConfigurationDocument(new SetConfigurationDocumentRequest(documentKey, document));
                        }
                        else
                        {
                            // every value is the same as the default, so the document can be removed
                            // update local cache and server
                            offlineCacheClient.Remove(documentKey);
                            service.RemoveConfigurationDocument(new RemoveConfigurationDocumentRequest(documentKey));
                        }
                    }
                    catch (EndpointNotFoundException e)
                    {
                        Platform.Log(LogLevel.Debug, e, "Unable to save settings to configuration service.");
                    }
                }
        }