/// <summary> /// /// </summary> /// <param name="sectionName"></param> /// <returns></returns> public object GetSection(string sectionName) { string xmlData; string configSectionType; using (SqlConnection myConnection = new SqlConnection(data.ConnectionString)) { try { // Create Instance of Connection and Command Object SqlCommand myCommand = new SqlCommand(data.GetStoredProcedure, myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlParameter parameterSectionName = new SqlParameter(@"@SectionName", SqlDbType.NVarChar); parameterSectionName.Value = sectionName; myCommand.Parameters.Add(parameterSectionName); // Execute the command myConnection.Open(); using (SqlDataReader sqlReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)) { if (!sqlReader.Read()) { return(null); } configSectionType = sqlReader.IsDBNull(0) ? null : sqlReader.GetString(0); xmlData = sqlReader.IsDBNull(0) ? null : sqlReader.GetString(1); } } catch (SqlException sqlException) { throw new ConfigurationErrorsException(String.Format(Resources.ExceptionConfigurationSqlInvalidSection, sectionName), sqlException); } } if (xmlData == null || xmlData.Trim().Equals(String.Empty)) { return(null); } //TODO: If data is encrypted, decrypt it here SerializableConfigurationSection configSection = (SerializableConfigurationSection)Activator.CreateInstance(Type.GetType(configSectionType)); XmlReaderSettings settings = new XmlReaderSettings(); settings.CloseInput = true; using (System.IO.StringReader stringReader = new System.IO.StringReader(xmlData)) { using (XmlReader reader = XmlReader.Create(stringReader, settings)) { configSection.ReadXml(reader); reader.Close(); } stringReader.Close(); } return(configSection); }
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); } }