コード例 #1
0
        //
        // Add a section definition update to the correct location update.
        //
        internal DefinitionUpdate AddUpdate(OverrideModeSetting overrideMode, bool inheritInChildApps, bool moved, string updatedXml, SectionRecord sectionRecord) {
            LocationUpdates locationUpdates = FindLocationUpdates(overrideMode, inheritInChildApps);
            if (locationUpdates == null) {
                locationUpdates = new LocationUpdates(overrideMode, inheritInChildApps);
                _locationUpdatesList.Add(locationUpdates);
            }

            DefinitionUpdate definitionUpdate = new DefinitionUpdate(sectionRecord.ConfigKey, moved, updatedXml, sectionRecord);
            locationUpdates.SectionUpdates.AddSection(definitionUpdate);
            return definitionUpdate;
        }
コード例 #2
0
        internal DefinitionUpdate AddUpdate(OverrideModeSetting overrideMode, bool inheritInChildApps, bool moved, string updatedXml, SectionRecord sectionRecord)
        {
            LocationUpdates updates = this.FindLocationUpdates(overrideMode, inheritInChildApps);

            if (updates == null)
            {
                updates = new LocationUpdates(overrideMode, inheritInChildApps);
                this._locationUpdatesList.Add(updates);
            }
            DefinitionUpdate update = new DefinitionUpdate(sectionRecord.ConfigKey, moved, updatedXml, sectionRecord);

            updates.SectionUpdates.AddSection(update);
            return(update);
        }
コード例 #3
0
        //
        // Add a section definition update to the correct location update.
        //
        internal DefinitionUpdate AddUpdate(bool allowOverride, bool inheritInChildApps, bool moved, string updatedXml, SectionRecord sectionRecord)
        {
            LocationUpdates locationUpdates = FindLocationUpdates(allowOverride, inheritInChildApps);

            if (locationUpdates == null)
            {
                locationUpdates = new LocationUpdates(allowOverride, inheritInChildApps);
                _locationUpdatesList.Add(locationUpdates);
            }

            DefinitionUpdate definitionUpdate = new DefinitionUpdate(sectionRecord.ConfigKey, moved, updatedXml, sectionRecord);

            locationUpdates.SectionUpdates.AddSection(definitionUpdate);
            return(definitionUpdate);
        }
コード例 #4
0
        //
        // SaveConfigSource
        //
        private void SaveConfigSource(DefinitionUpdate update) {

            string configSourceStreamName;

            if (update.SectionRecord.HasResult) {
                ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
                configSourceStreamName = configSection.SectionInformation.ConfigSourceStreamName;
            }
            else {
                Debug.Assert(update.SectionRecord.HasFileInput, "update.SectionRecord.HasFileInput");
                SectionInput fileInput = update.SectionRecord.FileInput;
                configSourceStreamName = fileInput.SectionXmlInfo.ConfigSourceStreamName;
            }

            // Copy the input stream before opening the output stream.
            byte[] readBuffer = null;
            using (Stream streamRead = Host.OpenStreamForRead(configSourceStreamName)) {
                if (streamRead != null) {
                    readBuffer = new byte[streamRead.Length];
                    int count = streamRead.Read(readBuffer, 0, (int) streamRead.Length);
                    if (count != streamRead.Length) {
                        throw new ConfigurationErrorsException();
                    }
                }
            }

            // Write the changes to the output stream.
            bool hasFile = (readBuffer != null);
            object writeContext = null;
            bool streamOpened = false;

            try {
                try {
                    string templateStreamName;

                    if (Host.IsRemote) {
                        // templateStreamName is used by OpenStreamForWrite for copying file attributes during saving.
                        // (for details, see WriteFileContext.Complete.)
                        //
                        // If we're using a remote host, then ConfigStreamInfo.StreamName is actually pointing to a
                        // full filepath on a remote machine.  In this case, it's impossible to copy the attributes
                        // over, and thus we won't do it.
                        templateStreamName = null;
                    }
                    else {
                        templateStreamName = ConfigStreamInfo.StreamName;
                    }

                    using (Stream streamWrite = Host.OpenStreamForWrite(configSourceStreamName, templateStreamName, ref writeContext)) {
                        streamOpened = true;
                        if (update.UpdatedXml == null) {
                            Debug.Assert(hasFile, "hasFile");
                            if (hasFile) {
                                streamWrite.Write(readBuffer, 0, readBuffer.Length);
                            }
                        }
                        else {
                            using (StreamWriter streamWriter = new StreamWriter(streamWrite)) {
                                XmlUtilWriter utilWriter = new XmlUtilWriter(streamWriter, true);
                                if (hasFile) {
                                    CopyConfigSource(utilWriter, update.UpdatedXml, configSourceStreamName, readBuffer);
                                }
                                else {
                                    CreateNewConfigSource(utilWriter, update.UpdatedXml, DEFAULT_INDENT);
                                }
                            }
                        }
                    }
                }
                catch {
                    if (streamOpened) {
                        Host.WriteCompleted(configSourceStreamName, false, writeContext);
                    }

                    throw;
                }
            }
            //
            // Guarantee that exceptions contain at least the name of the stream by wrapping them
            // in a ConfigurationException.
            //
            catch (Exception e) {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, configSourceStreamName, 0);
            }

            Host.WriteCompleted(configSourceStreamName, true, writeContext);
        }
コード例 #5
0
        private void WriteSectionUpdate(XmlUtilWriter utilWriter, DefinitionUpdate update, int linePosition, int indent, bool skipFirstIndent) {
            ConfigurationSection configSection = (ConfigurationSection) update.SectionRecord.Result;
            string updatedXml;

            if (!String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) {
                updatedXml = string.Format(CultureInfo.InvariantCulture, FORMAT_SECTION_CONFIGSOURCE, configSection.SectionInformation.Name, configSection.SectionInformation.ConfigSource);
            }
            else {
                updatedXml = update.UpdatedXml;
            }

            string formattedXml = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, skipFirstIndent);
            utilWriter.Write(formattedXml);
        }
 private void WriteSectionUpdate(XmlUtilWriter utilWriter, DefinitionUpdate update, int linePosition, int indent, bool skipFirstIndent)
 {
     string updatedXml;
     ConfigurationSection result = (ConfigurationSection) update.SectionRecord.Result;
     if (!string.IsNullOrEmpty(result.SectionInformation.ConfigSource))
     {
         updatedXml = string.Format(CultureInfo.InvariantCulture, "<{0} configSource=\"{1}\" />", new object[] { result.SectionInformation.Name, result.SectionInformation.ConfigSource });
     }
     else
     {
         updatedXml = update.UpdatedXml;
     }
     string s = XmlUtil.FormatXmlElement(updatedXml, linePosition, indent, skipFirstIndent);
     utilWriter.Write(s);
 }
 private void SaveConfigSource(DefinitionUpdate update)
 {
     string configSourceStreamName;
     if (update.SectionRecord.HasResult)
     {
         ConfigurationSection result = (ConfigurationSection) update.SectionRecord.Result;
         configSourceStreamName = result.SectionInformation.ConfigSourceStreamName;
     }
     else
     {
         configSourceStreamName = update.SectionRecord.FileInput.SectionXmlInfo.ConfigSourceStreamName;
     }
     byte[] buffer = null;
     using (Stream stream = base.Host.OpenStreamForRead(configSourceStreamName))
     {
         if (stream != null)
         {
             buffer = new byte[stream.Length];
             if (stream.Read(buffer, 0, (int) stream.Length) != stream.Length)
             {
                 throw new ConfigurationErrorsException();
             }
         }
     }
     bool flag = buffer != null;
     object writeContext = null;
     bool flag2 = false;
     try
     {
         try
         {
             string streamName;
             if (base.Host.IsRemote)
             {
                 streamName = null;
             }
             else
             {
                 streamName = base.ConfigStreamInfo.StreamName;
             }
             using (Stream stream2 = base.Host.OpenStreamForWrite(configSourceStreamName, streamName, ref writeContext))
             {
                 flag2 = true;
                 if (update.UpdatedXml == null)
                 {
                     if (flag)
                     {
                         stream2.Write(buffer, 0, buffer.Length);
                     }
                 }
                 else
                 {
                     using (StreamWriter writer = new StreamWriter(stream2))
                     {
                         XmlUtilWriter utilWriter = new XmlUtilWriter(writer, true);
                         if (flag)
                         {
                             this.CopyConfigSource(utilWriter, update.UpdatedXml, configSourceStreamName, buffer);
                         }
                         else
                         {
                             this.CreateNewConfigSource(utilWriter, update.UpdatedXml, 4);
                         }
                     }
                 }
             }
         }
         catch
         {
             if (flag2)
             {
                 base.Host.WriteCompleted(configSourceStreamName, false, writeContext);
             }
             throw;
         }
     }
     catch (Exception exception)
     {
         throw ExceptionUtil.WrapAsConfigException(System.Configuration.SR.GetString("Config_error_loading_XML_file"), exception, configSourceStreamName, 0);
     }
     base.Host.WriteCompleted(configSourceStreamName, true, writeContext);
 }