Пример #1
0
        public void ValidateConfigurationSectionFactory()
        {
            ConfigurationSection configSection = ConfigurationSectionFactory.GetSection(StorageAccountConfigurationSettings.SectionName);

            Assert.IsNotNull(configSection, "Config section is null");
            Assert.AreEqual <Type>(typeof(StorageAccountConfigurationSettings), configSection.GetType(), "Config section is of a wrong type");

            configSection = ConfigurationSectionFactory.GetSection(typeof(RetryPolicyConfigurationSettings).FullName);

            Assert.IsNotNull(configSection, "Config section is null");
            Assert.AreEqual <Type>(typeof(RetryPolicyConfigurationSettings), configSection.GetType(), "Config section is of a wrong type");

            configSection = ConfigurationSectionFactory.GetSection(typeof(ApplicationDiagnosticSettings).AssemblyQualifiedName);

            Assert.IsNotNull(configSection, "Config section is null");
            Assert.AreEqual <Type>(typeof(ApplicationDiagnosticSettings), configSection.GetType(), "Config section is of a wrong type");
        }
Пример #2
0
        /// <summary>
        /// Retrieves the specified <see cref="System.Configuration.ConfigurationSection"/> for a given application running of the specified machine.
        /// </summary>
        /// <param name="sectionName">The name of the section to be retrieved.</param>
        /// <param name="applicationName">The name of the application for which a configuration section is being requested.</param>
        /// <param name="machineName">The name of the machine on which the requesting application is running.</param>
        /// <returns>The specified <see cref="System.Configuration.ConfigurationSection"/>, or a null reference if a section by that name is not found.</returns>
        public ConfigurationSection GetSection(string sectionName, string applicationName, string machineName)
        {
            var callToken = TraceManager.RulesComponent.TraceIn(sectionName, this.configPolicy, this.configVersion);

            try
            {
                PolicyExecutionInfo policyExecInfo = new PolicyExecutionInfo(PolicyName, PolicyVersion);

                policyExecInfo.AddParameter(WellKnownContractMember.MessageParameters.SectionName, sectionName);
                policyExecInfo.AddParameter(WellKnownContractMember.MessageParameters.ApplicationName, applicationName);
                policyExecInfo.AddParameter(WellKnownContractMember.MessageParameters.MachineName, machineName);

                ConfigurationSection configSection = ConfigurationSectionFactory.GetSection(sectionName);

                if (configSection != null)
                {
                    PolicyExecutionResult execResult = PolicyHelper.Execute(policyExecInfo, RulesEngineFactFactory.GetFacts(configSection));

                    if (execResult.Success)
                    {
                        // If policy invocation was successful, we need to update the policy version number of reflect the true version of the policy.
                        this.actualConfigVersion = new Version(execResult.PolicyVersion.Major, execResult.PolicyVersion.Minor);

                        if (SourceChanged != null)
                        {
                            SourceChanged(this, new ConfigurationSourceChangedEventArgs(this, new string[] { sectionName }));
                        }

                        return(configSection);
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.ConfigPolicyExecutionFailed, execResult.PolicyName), execResult.Errors.Count > 0 ? execResult.Errors[0] : null);
                    }
                }
                else
                {
                    throw new ConfigurationErrorsException(String.Format(CultureInfo.CurrentCulture, ExceptionMessages.ConfigurationSectionNotSupported, sectionName, Resources.RulesEngineConfigurationSourceElementDisplayName));
                }
            }
            finally
            {
                TraceManager.RulesComponent.TraceOut(callToken);
            }
        }
Пример #3
0
        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);
            }
        }