Example #1
0
        /// <summary>
        /// Loads and validates the application configuration from a configuration section.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="applicationType">Type of the application.</param>
        /// <param name="systemType">Type of the system.</param>
        /// <param name="applyTraceSettings">if set to <c>true</c> apply trace settings after validation.</param>
        /// <returns>Application configuration</returns>
        public static ApplicationConfiguration Load(FileInfo file, ApplicationType applicationType, Type systemType, bool applyTraceSettings)
        {
            ApplicationConfiguration configuration = null;

            if (systemType == null)
            {
                systemType = typeof(ApplicationConfiguration);
            }

            XmlTextReader reader = new XmlTextReader(file.Open(FileMode.Open, FileAccess.Read));

            try
            {
                DataContractSerializer serializer = new DataContractSerializer(systemType);
                configuration = serializer.ReadObject(reader, false) as ApplicationConfiguration;
            }
            catch (Exception e)
            {
                throw ServiceResultException.Create(
                          StatusCodes.BadConfigurationError,
                          e,
                          "Configuration file could not be loaded: {0}\r\nError is: {1}",
                          file.FullName,
                          e.Message);
            }
            finally
            {
                reader.Close();
            }

            if (configuration != null)
            {
                // should not be here but need to preserve old behavoir.
                if (applyTraceSettings && configuration.TraceConfiguration != null)
                {
                    configuration.TraceConfiguration.ApplySettings();
                }

                configuration.Validate(applicationType);
                configuration.m_sourceFilePath = file.FullName;
            }

            return(configuration);
        }
        /// <summary>
        /// Loads and validates the application configuration from a configuration section.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="applicationType">Type of the application.</param>
        /// <param name="systemType">Type of the system.</param>
        /// <param name="applyTraceSettings">if set to <c>true</c> apply trace settings after validation.</param>
        /// <param name="certificatePasswordProvider">The certificate password provider.</param>
        /// <returns>Application configuration</returns>
        public static async Task <ApplicationConfiguration> Load(
            Stream stream,
            ApplicationType applicationType,
            Type systemType,
            bool applyTraceSettings,
            ICertificatePasswordProvider certificatePasswordProvider = null)
        {
            ApplicationConfiguration configuration = null;

            systemType = systemType ?? typeof(ApplicationConfiguration);

            try
            {
                DataContractSerializer serializer = new DataContractSerializer(systemType);
                configuration = (ApplicationConfiguration)serializer.ReadObject(stream);
            }
            catch (Exception e)
            {
                var message = new StringBuilder();
                message.AppendFormat("Configuration could not be loaded.");
                message.AppendLine();
                message.AppendFormat("Error is: {0}", e.Message);
                throw ServiceResultException.Create(
                          StatusCodes.BadConfigurationError, e, message.ToString());
            }

            if (configuration != null)
            {
                // should not be here but need to preserve old behavior.
                if (applyTraceSettings && configuration.TraceConfiguration != null)
                {
                    configuration.TraceConfiguration.ApplySettings();
                }

                configuration.SecurityConfiguration.CertificatePasswordProvider = certificatePasswordProvider;

                await configuration.Validate(applicationType).ConfigureAwait(false);
            }

            return(configuration);
        }
Example #3
0
        /// <summary>
        /// Loads and validates the application configuration from a configuration section.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="applicationType">Type of the application.</param>
        /// <param name="systemType">Type of the system.</param>
        /// <param name="applyTraceSettings">if set to <c>true</c> apply trace settings after validation.</param>
        /// <returns>Application configuration</returns>
        public static async Task <ApplicationConfiguration> Load(FileInfo file, ApplicationType applicationType, Type systemType, bool applyTraceSettings)
        {
            ApplicationConfiguration configuration = null;

            systemType = systemType ?? typeof(ApplicationConfiguration);

            using (FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    DataContractSerializer serializer = new DataContractSerializer(systemType);
                    configuration = (ApplicationConfiguration)serializer.ReadObject(stream);
                }
                catch (Exception e)
                {
                    throw ServiceResultException.Create(
                              StatusCodes.BadConfigurationError,
                              e,
                              "Configuration file could not be loaded: {0}\r\nError is: {1}",
                              file.FullName,
                              e.Message);
                }
            }

            if (configuration != null)
            {
                // should not be here but need to preserve old behavior.
                if (applyTraceSettings && configuration.TraceConfiguration != null)
                {
                    configuration.TraceConfiguration.ApplySettings();
                }

                await configuration.Validate(applicationType);

                configuration.m_sourceFilePath = file.FullName;
            }

            return(configuration);
        }