private void DeserializeSection(ApplicationConfigurationSettings source) { using (MemoryStream memoryBuffer = new MemoryStream()) { XmlWriterSettings writerRettings = new XmlWriterSettings(); writerRettings.CloseOutput = false; writerRettings.CheckCharacters = false; writerRettings.ConformanceLevel = ConformanceLevel.Fragment; writerRettings.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter writer = XmlWriter.Create(memoryBuffer, writerRettings)) { source.WriteXml(writer); writer.Flush(); } memoryBuffer.Seek(0, SeekOrigin.Begin); XmlDocument configXml = FrameworkUtility.CreateXmlDocument(memoryBuffer); XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.CloseInput = false; readerSettings.IgnoreWhitespace = true; readerSettings.IgnoreComments = true; readerSettings.ValidationType = ValidationType.None; readerSettings.IgnoreProcessingInstructions = true; using (XmlReader reader = XmlReader.Create(new StringReader(configXml.OuterXml), readerSettings)) { this.ReadXml(reader); } this.PostDeserialize(); } }
private ConfigurationSection RetrieveSection(string sectionName) { var callToken = TraceManager.DebugComponent.TraceIn(sectionName); try { using (ReliableServiceBusClient <IOnPremiseConfigurationServiceChannel> configServiceClient = new ReliableServiceBusClient <IOnPremiseConfigurationServiceChannel>(this.sbEndpointInfo, this.retryPolicy)) { var startScopeInvokeService = TraceManager.DebugComponent.TraceStartScope(Resources.ScopeOnPremiseConfigurationSourceInvokeService, callToken); try { // Invoke the WCF service in a reliable fashion and retrieve the specified configuration section. XmlElement configSectionXml = configServiceClient.RetryPolicy.ExecuteAction <XmlElement>(() => { return(configServiceClient.Client.GetConfigurationSection(sectionName, CloudEnvironment.CurrentRoleName, CloudEnvironment.CurrentRoleMachineName)); }); if (configSectionXml != null) { // Instantiate a configuration object that correspond to the specified section. ConfigurationSection configSection = ConfigurationSectionFactory.GetSection(sectionName); // Gotcha: configuration section deserializer requires a well-formed XML document including processing instruction. XmlDocument configXml = FrameworkUtility.CreateXmlDocument(); configXml.AppendChild(configXml.ImportNode(configSectionXml, true)); // Configure XML reader settings to disable validation and ignore certain XML entities. XmlReaderSettings settings = new XmlReaderSettings { CloseInput = true, IgnoreWhitespace = true, IgnoreComments = true, ValidationType = ValidationType.None, IgnoreProcessingInstructions = true }; // Create a reader to consume the XML data. using (XmlReader reader = XmlReader.Create(new StringReader(configXml.OuterXml), settings)) { // Attempt to cast the configuration section object into SerializableConfigurationSection for further check. SerializableConfigurationSection serializableSection = configSection as SerializableConfigurationSection; // Check if the the configuration section natively supports serialization/de-serialization. if (serializableSection != null) { // Yes, it's supported. Invoke the ReadXml method to consume XML and turn it into object model. serializableSection.ReadXml(reader); } else { // No, it's unsupported. Need to do something different, starting with positioning the XML reader to the first available node. reader.Read(); // Invoke the DeserializeSection method via reflection. This is the only way as the method is internal. MethodInfo info = configSection.GetType().GetMethod(WellKnownContractMember.MethodNames.DeserializeSection, BindingFlags.NonPublic | BindingFlags.Instance); info.Invoke(configSection, new object[] { reader }); } reader.Close(); } if (SourceChanged != null) { SourceChanged(this, new ConfigurationSourceChangedEventArgs(this, new string[] { sectionName })); } return(configSection); } else { // The specified section is not supported by the remote configuration source. We should not throw an exception and rely on the caller to handle an empty section. return(null); } } finally { TraceManager.DebugComponent.TraceEndScope(Resources.ScopeOnPremiseConfigurationSourceInvokeService, startScopeInvokeService, callToken); } } } catch (Exception ex) { TraceManager.DebugComponent.TraceError(ex, callToken); throw; } finally { TraceManager.DebugComponent.TraceOut(callToken); } }