コード例 #1
0
        /// <summary>
        ///     Parse the content of a web.config file.
        /// </summary>
        /// <param name="content">File Content to be Parsed</param>
        /// <param name="path">Path or filename</param>
        /// <returns>Parsed Output</returns>
        public static WebConfigFile ParseWebConfigFileContent(string content, string path)
        {
            Validators.AssertIsNotNullOrWhitespace(content, nameof(content));

            var output = new WebConfigFile();

            var document = XElement.Parse(content);

            output.AppSettings = GetAppSettings(document);

            var environment = GetEnvironmentFromPath(path);

            SetDeployEnvironment(output.AppSettings, environment);

            return(output);
        }
コード例 #2
0
        public static SwarmDb GetDatabaseAsAdmin()
        {
            string connectionString = string.Empty;

            try
            {
                if (Path.DirectorySeparatorChar == '/' && HttpContext.Current == null)
                {
                    // We are running under mono in a backend environment

                    using (StreamReader reader = new StreamReader(MonoConfigFile.Replace(".config", "-admin.config")))
                    {
                        connectionString = reader.ReadLine();

                        Logging.LogInformation(LogSource.PirateDb,
                                               "SwarmDb initialized for Linux Backend: [" + connectionString + "]");
                    }
                }
                else if (HttpContext.Current != null)
                {
                    // We are running a web application, under Mono (production) or Windows (development)
                    using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath(WebConfigFile.Replace(".config", "-admin.config")))
                           )
                    {
                        connectionString = reader.ReadLine();

                        Logging.LogInformation(LogSource.PirateDb,
                                               "SwarmDb initialized for web: [" + connectionString + "]");
                    }
                }
                else
                {
                    // We are running an application, presumably directly from Visual Studio.
                    // If so, the current working directory is "PirateWeb/30/Console/bin".
                    using (StreamReader reader = new StreamReader(AppConfigFile.Replace(".config", "-admin.config")))
                    {
                        connectionString = reader.ReadLine();

                        Logging.LogInformation(LogSource.PirateDb,
                                               "SwarmDb initialized for application: [" + connectionString +
                                               "]");
                    }
                }
            }
            catch (Exception)
            {
                Logging.LogWarning(LogSource.PirateDb, "Unable to read Database.Config - defaulting");
                // Ignore if we can't read the Database.config
            }

            // To simplify future checks
            if (connectionString != null && connectionString.Length == 0)
            {
                connectionString = null;
            }

            // If we still have nothing, and we're running from web, then assume we have a dev environment and use the hostname as db, user, and pass.
            if (String.IsNullOrEmpty(connectionString))
            {
                if (HttpContext.Current != null) // dummy comment to force build, remove on sight
                {
                    string hostName = HttpContext.Current.Request.Url.Host;

                    connectionString = "server=peregrine;database=" + hostName + ";user="******"-admin;password="******"-admin";  // TODO: Replace "peregrine" with "localhost"
                }
                else
                {
                    throw new InvalidOperationException("No database-as-admin connection string found -- write a connect string into the \"ActivizrAdminConnect\" environment var, or on one line into a file named database-admin.config; see connectionstrings.com for examples");  // TODO: Replace with custom exception to present config screen
                }
            }

            // Now write the correct data to the cache, for faster lookup next time
            if (_cachedConnectionString == null)
            {
                _cachedConnectionString = connectionString;
            }

            return(new SwarmDb(DbProviderFactories.GetFactory(DefaultProviderName), connectionString));
        }