public ConfigurationSection GetSection(string sectionName) { ConfigurationSection configSection; if (sections.TryGetValue(sectionName, out configSection)) { SerializableConfigurationSection section = configSection as SerializableConfigurationSection; if (section != null) { using (StringWriter xml = new StringWriter()) using (XmlWriter xmlwriter = XmlWriter.Create(xml)) { section.WriteXml(xmlwriter); xmlwriter.Flush(); MethodInfo methodInfo = section.GetType().GetMethod("DeserializeSection", BindingFlags.NonPublic | BindingFlags.Instance); methodInfo.Invoke(section, new object[] { XDocument.Parse(xml.ToString()).CreateReader() }); return(configSection); } } } return(null); }
/// <summary> /// /// </summary> /// <param name="sectionName"></param> /// <param name="section"></param> /// <param name="data"></param> public static void SaveSection(string sectionName, SerializableConfigurationSection section, SqlConfigurationData data) { if (!string.IsNullOrEmpty(sectionName)) { SqlConfigurationManager.PrepareConfigSystem(data); SqlConfigurationManager.configSystem.SaveSection(sectionName, section); } }
/// <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); }
/// <summary> /// /// </summary> /// <param name="saveParameter"></param> /// <param name="sectionName"></param> /// <param name="configurationSection"></param> public void Add(IConfigurationParameter saveParameter, string sectionName, ConfigurationSection configurationSection) { if (null == saveParameter) { throw new ArgumentException(string.Format(Resources.Culture, Resources.ExceptionUnexpectedType, typeof(SqlConfigurationParameter).Name), "saveParameter"); } if (!(configurationSection is SerializableConfigurationSection)) { throw new ArgumentException(string.Format(Resources.Culture, Resources.ExceptionUnexpectedType, typeof(SerializableConfigurationSection).Name), "configurationSection"); } SqlConfigurationParameter parameter = saveParameter as SqlConfigurationParameter; SerializableConfigurationSection serializableSection = configurationSection as SerializableConfigurationSection; Save(parameter.ConnectionString, parameter.SetStoredProcedure, sectionName, serializableSection); }
/// <summary> /// /// </summary> public void SaveSection(string sectionName, SerializableConfigurationSection configurationSection) { //TODO: if encryption enabled, encrypt it here // Create Instance of Connection and Command Object using (SqlConnection myConnection = new SqlConnection(data.ConnectionString)) { try { SqlCommand myCommand = new SqlCommand(data.SetStoredProcedure, myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlParameter sectionNameParameter = new SqlParameter(@"@section_name", SqlDbType.NVarChar); sectionNameParameter.Value = sectionName; myCommand.Parameters.Add(sectionNameParameter); SqlParameter sectionTypeParameter = new SqlParameter(@"@section_type", SqlDbType.NVarChar); sectionTypeParameter.Value = configurationSection.GetType().AssemblyQualifiedName; myCommand.Parameters.Add(sectionTypeParameter); SqlParameter sectionValueParameter = new SqlParameter(@"@section_value", SqlDbType.NText); StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); using (XmlWriter writer = XmlWriter.Create(output, settings)) { configurationSection.WriteXml(writer); writer.Close(); writer.Flush(); } sectionValueParameter.Value = output.ToString(); myCommand.Parameters.Add(sectionValueParameter); // Execute the command myConnection.Open(); myCommand.ExecuteNonQuery(); } catch (Exception e) { throw new ConfigurationErrorsException(Resources.ExceptionConfigurationCannotSet, e); } } }
/// <summary> /// Saves a section from SqlConfiguration, and starts watching for /// its changes if not watching already. /// </summary> /// <param name="sectionName">The section name.</param> /// <param name="section">The section.</param> /// <returns></returns> public void SaveSection(string sectionName, SerializableConfigurationSection section) { if (section == null) { throw new ArgumentNullException("section"); } if ((sectionName == null) || (sectionName.Trim().Equals(String.Empty))) { throw new ArgumentNullException(sectionName); } SqlConfigurationManager.SaveSection(sectionName, section, data); lock (lockMe) { if (!IsWatchingSection(sectionName)) { SetWatcherForSection(sectionName, section.SectionInformation.ConfigSource); } } }
/// <summary> /// Executes the custom runtime task component to process the input message and returns the result message. /// </summary> /// <param name="pContext">A reference to <see cref="Microsoft.BizTalk.Component.Interop.IPipelineContext"/> object that contains the current pipeline context.</param> /// <param name="pInMsg">A reference to <see cref="Microsoft.BizTalk.Message.Interop.IBaseMessage"/> object that contains the message to process.</param> /// <returns>A reference to the returned <see cref="Microsoft.BizTalk.Message.Interop.IBaseMessage"/> object which will contain the output message.</returns> public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg) { Guard.ArgumentNotNull(pContext, "pContext"); Guard.ArgumentNotNull(pInMsg, "pInMsg"); var callToken = TraceManager.PipelineComponent.TraceIn(); // It is OK to load the entire message into XML DOM. The size of these requests is anticipated to be very small. XDocument request = XDocument.Load(pInMsg.BodyPart.GetOriginalDataStream()); string sectionName = (from childNode in request.Root.Descendants() where childNode.Name.LocalName == WellKnownContractMember.MessageParameters.SectionName select childNode.Value).FirstOrDefault <string>(); string applicationName = (from childNode in request.Root.Descendants() where childNode.Name.LocalName == WellKnownContractMember.MessageParameters.ApplicationName select childNode.Value).FirstOrDefault <string>(); string machineName = (from childNode in request.Root.Descendants() where childNode.Name.LocalName == WellKnownContractMember.MessageParameters.MachineName select childNode.Value).FirstOrDefault <string>(); TraceManager.PipelineComponent.TraceInfo(TraceLogMessages.GetConfigSectionRequest, sectionName, applicationName, machineName); IConfigurationSource configSource = ApplicationConfiguration.Current.Source; IApplicationConfigurationSource appConfigSource = configSource as IApplicationConfigurationSource; ConfigurationSection configSection = appConfigSource != null?appConfigSource.GetSection(sectionName, applicationName, machineName) : configSource.GetSection(sectionName); if (configSection != null) { IBaseMessagePart responsePart = BizTalkUtility.CreateResponsePart(pContext.GetMessageFactory(), pInMsg); XmlWriterSettings settings = new XmlWriterSettings(); MemoryStream dataStream = new MemoryStream(); pContext.ResourceTracker.AddResource(dataStream); settings.CloseOutput = false; settings.CheckCharacters = false; settings.ConformanceLevel = ConformanceLevel.Fragment; settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter configDataWriter = XmlWriter.Create(dataStream, settings)) { configDataWriter.WriteResponseStartElement("r", WellKnownContractMember.MethodNames.GetConfigurationSection, WellKnownNamespace.ServiceContracts.General); configDataWriter.WriteResultStartElement("r", WellKnownContractMember.MethodNames.GetConfigurationSection, WellKnownNamespace.ServiceContracts.General); SerializableConfigurationSection serializableSection = configSection as SerializableConfigurationSection; if (serializableSection != null) { serializableSection.WriteXml(configDataWriter); } else { MethodInfo info = configSection.GetType().GetMethod(WellKnownContractMember.MethodNames.SerializeSection, BindingFlags.NonPublic | BindingFlags.Instance); string serialized = (string)info.Invoke(configSection, new object[] { configSection, sectionName, ConfigurationSaveMode.Full }); configDataWriter.WriteRaw(serialized); } configDataWriter.WriteEndElement(); configDataWriter.WriteEndElement(); configDataWriter.Flush(); } dataStream.Seek(0, SeekOrigin.Begin); responsePart.Data = dataStream; } else { throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.ConfigurationSectionNotFound, sectionName, ApplicationConfiguration.Current.Source.GetType().FullName)); } TraceManager.PipelineComponent.TraceOut(callToken); return(pInMsg); }
/// <summary> /// /// </summary> /// <param name="connectionString"></param> /// <param name="setStoredProcedure"></param> /// <param name="section"></param> /// <param name="configurationSection"></param> public void Save(string connectionString, string setStoredProcedure, string section, SerializableConfigurationSection configurationSection) { ValidateArgumentsAndConnectionInfo(connectionString, setStoredProcedure, section, configurationSection); //TODO: need to modify this to use arguments passed in -- can I make modifications to just call SqlConfigurationManager.SaveSection(connectionString, setStoredProcedure, section, configurationSection); implementationByConnectionString[connectionString].SaveSection(section, configurationSection); }
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); } }