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);
        }
Example #2
0
        /// <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);
                }
            }
        }
Example #3
0
        /// <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);
        }