コード例 #1
0
        /// <summary>
        /// Configures a framework construction in the default way
        /// </summary>
        /// <param name="construction">The construction to configure</param>
        /// <param name="configure">The custom configuration action</param>
        /// <returns></returns>
        public static FrameworkConstruction AddDefaultConfiguration(this FrameworkConstruction construction, Action <IConfigurationBuilder> configure = null)
        {
            // Create our configuration sources
            var configurationBuilder = new ConfigurationBuilder()
                                       // Add environment variables
                                       .AddEnvironmentVariables();

            // If we are not on a mobile platform...
            if (!construction.Environment.IsMobile)
            {
                // Add file based configuration
                // In the ASP.NET Core application the settings file are loaded from the app's content root by calling CreateDefaultBuilder during host construction.
                // For other .net application, such as WPF application we need set base path for Json files as the startup location of the application
                configurationBuilder.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                // Add application settings json files
                configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                configurationBuilder.AddJsonFile($"appsettings.{construction.Environment.Configuration}.json", true, true);
            }

            // Let custom configuration happen
            configure?.Invoke(configurationBuilder);

            // Build configuration like BuildHostConfiguration() included by ASP.NET Core IHostBuilder.Build() method.
            var configuration = configurationBuilder.Build();

            // Inject configuration into services
            construction.Services.AddSingleton <IConfiguration>(configuration);

            // Set the construction configuration
            construction.UseConfiguration(configuration);

            // Chain the construction
            return(construction);
        }
コード例 #2
0
        /// <summary>
        /// Configures a framework construction using the provided configuration
        /// </summary>
        /// <param name="construction">The construction to configure</param>
        /// <param name="configuration">The configuration</param>
        /// <returns></returns>
        public static FrameworkConstruction AddConfiguration(this FrameworkConstruction construction, IConfiguration configuration)
        {
            // Add specific configuration
            construction.UseConfiguration(configuration);

            // Add configuration to services
            construction.Services.AddSingleton(configuration);

            // Chain the construction
            return(construction);
        }