Ejemplo n.º 1
0
        private void Configure()
        {
            const string mainFileName = "application.config";
            const string prefix       = "application-";
            const string suffix       = ".config";

            string envFileName  = prefix + RuntimeEnvironment.GetApplicationEnvironmentName(_environment) + suffix;
            string hostFileName = prefix + RuntimeEnvironment.HostName + suffix;

            LoadApplicationPropertyPath();

            // Read property files as follows (for more details see "GuideToLinkMeConfiguration" on the Wiki):
            // 1) Embedded "application.config"
            // 2) Embedded "application-ENV.config" (the same setting must not be defined in 1 and 2).
            // 3) externalPropertyPath\"application-ENV.config" (overrides whatever is defined in 1 and 2.
            // 4) externalPropertyPath\"application-HOSTNAME.config" (overrides whatever is defined in 1 and 2, but
            // the same setting must not be defined in 3 and 4).
            // 5) sourceRootPath\config\"application-ENV.config" (overrides whatever is defined in 1, 2, 3 and 4.
            // 6) sourceRootPath\config\"application-HOSTNAME.config" (overrides whatever is defined in
            // 1, 2, 3 and 4, but the same setting must not be defined in 5 and 6).

            _sources = new SortedList <string, string>();

            IDictionary <string, string> embeddedMain = LoadEmbeddedProperties(mainFileName); // 1
            IDictionary <string, string> embeddedEnv  = LoadEmbeddedProperties(envFileName);  // 2

            string[] overridden;
            IDictionary <string, string> tempProperties = CombineProperties(embeddedMain, embeddedEnv,
                                                                            out overridden);

            if (overridden != null)
            {
                throw new ApplicationException(string.Format("The following configuration properties"
                                                             + " were defined in both '{0}' (embedded) and '{1}' (embedded): '{2}'. A given"
                                                             + " property is either environment-specific or not, so it must be defined either in"
                                                             + " '{0}' or in EACH of the environment-specific files.", mainFileName, envFileName,
                                                             string.Join("', '", overridden)));
            }

            IDictionary <string, string> externalProperties = GetExternalProperties(RuntimeEnvironment.HostName,
                                                                                    _externalPropertyPath, envFileName, hostFileName); // 3 + 4

            // Combine the embedded and external properties into (external config files override the
            // embedded ones).

            _properties = CombineProperties(tempProperties, externalProperties, out overridden);

            var sourceFolder = RuntimeEnvironment.GetSourceFolder();

            if (sourceFolder != null)
            {
                string sourceConfigPath = Path.Combine(sourceFolder, "config");
                IDictionary <string, string> sourceProperties = GetExternalProperties(
                    RuntimeEnvironment.HostName, sourceConfigPath, envFileName, hostFileName); // 5 + 6

                // Add in the the properties under <source root>\config, overriding anything else.

                string[] dummy;
                _properties = CombineProperties(_properties, sourceProperties, out dummy);
            }
        }