コード例 #1
0
        public static IHostBuilder CreateHostBuilder <TApp>(string[] args, ConfigureLocalServices <TApp> localServiceConfiguration = null) where TApp : class
        {
            IUserConfiguration userConfiguration = ConfigFactory.Initialize <TApp>();

            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args)
                                       .ConfigureServices((hostingContext, services) =>
            {
                localServiceConfiguration?.Invoke(hostingContext, services);

                services
                .AddTransient <TApp>()
                .AddSingleton <IApplicationRequirements <TApp>, ApplicationRequirements <TApp> >()
                .AddSingleton <IUserConfiguration> (sp =>
                {
                    return(userConfiguration);
                })
                .BuildServiceProvider();
            })
                                       .ConfigureLogging((hostingContext, logging) =>
            {
                ConfigureCustomLogging(hostingContext, logging, userConfiguration);
            });

            return(hostBuilder);
        }
コード例 #2
0
        /// <summary>
        /// Creates IOC container for console apps. Injects logging and custom configuration
        /// for application to consume in its constructor. The following example shows how
        /// to launch a class called "MyApplication" as your main application. It's constructor
        /// will have logging and configuration injected into it.
        ///
        ///             configuredApplication = ConsoleHostBuilderHelper.CreateApp<MyApplication>(args);
        ///             await configuredApplication.myService.Run();
        ///
        /// </summary>
        /// <typeparam name="TApp">Type of your main application class</typeparam>
        /// <param name="args">Any command line parameters you used to launch the console app are passed here</param>
        /// <param name="localServiceConfiguration">Delegate you can use to add more services to the IOC container</param>
        /// <returns>An ConfigurationResults object containing all the information about how this application is hosted</returns>
        public static ConfigurationResults <TApp> CreateApp <TApp>(string[] args, ConfigureLocalServices <TApp> localServiceConfiguration = null) where TApp : class
        {
            ConfigurationResults <TApp> config = new ConfigurationResults <TApp>();

            config.builder   = CreateHostBuilder <TApp>(args, localServiceConfiguration);
            config.myHost    = config.builder.Build();
            config.myService = config.myHost.Services.GetRequiredService <TApp>();
            return(config);
        }
コード例 #3
0
        /// <summary>
        /// Creates IOC container for console apps. Injects logging and custom configuration
        /// for application to consume in its constructor. The following example shows how
        /// to launch a class called "MyApplication" as your main application. It's constructor
        /// will have logging and configuration injected into it.
        ///
        ///     ConfigurationResults<MyApplication> _configuredApplication = CreateApp<MyApplication>(args);
        ///     await _configuredApplication.myService.Run();
        ///
        /// </summary>
        /// <typeparam name="TApp">Type of your main application class</typeparam>
        /// <param name="args">Any command line parameters you used to launch the console app are passed here</param>
        /// <param name="localServiceConfiguration">Delegate to be executed to add any non-standard configuration needs</param>
        /// <returns>An ConfigurationResults object containing all the information about how this application is hosted</returns>
        public static ConfigurationResults <TApp> CreateApp <TApp>(string[] args, ConfigureLocalServices <TApp> localServiceConfiguration = null) where TApp : class
        {
            ConfigurationResults <TApp> config = new ConfigurationResults <TApp>();

            config.builder                        = CreateHostBuilder <TApp>(args, localServiceConfiguration);
            config.myHost                         = config.builder.Build();
            config.myService                      = config.myHost.Services.GetRequiredService <TApp>();
            TraceLoggerExtension._Logger          = config.myHost.Services.GetRequiredService <ILogger <TApp> >();
            TraceLoggerExtension._environmentName = _appSetupConfig.RTE;
            return(config);
        }
        /// <summary>
        /// This method will create an initialize a generic Host Builder
        /// </summary>
        /// <typeparam name="TApp">Main application type. Used to access user secrets</typeparam>
        /// <param name="args">Application arguments</param>
        /// <param name="localServiceConfiguration">Delegate to be executed to add any non-standard configuration needs</param>
        /// <returns>Configured IHostBuilder</returns>
        public static IHostBuilder CreateHostBuilder <TApp>(string[] args, ConfigureLocalServices <TApp> localServiceConfiguration = null) where TApp : class
        {
            IApplicationSecrets            appSecrets      = null;
            IApplicationSetupConfiguration appIntialConfig = null;
            IAppConfigSections             sections        = null;

            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args)
                                       .ConfigureAppConfiguration((hostingContext, builder) =>
            {
                sections        = ConfigFactory.Initialize <TApp>(hostingContext, builder);
                appSecrets      = sections.appSecrets;
                appIntialConfig = sections.appIntialConfig;
            })
                                       .ConfigureServices((hostingContext, services) =>
            {
                localServiceConfiguration?.Invoke(hostingContext, services, sections);

                services
                .AddTransient <TApp>()
                .AddSingleton <IApplicationSetupConfiguration>(sp =>
                {
                    return(appIntialConfig);
                })
                .AddSingleton <IApplicationSecrets>(sp =>
                {
                    return(appSecrets);
                })
                .AddSingleton <IHostEnvironment>(sp =>
                {
                    return(hostingContext.HostingEnvironment);
                })
                .AddSingleton <IApplicationRequirements <TApp>, ApplicationRequirements <TApp> >();

                services.BuildServiceProvider();
            })
                                       .ConfigureLogging((hostingContext, logging) =>
            {
                ConfigureCustomLogging(hostingContext, logging, appSecrets, appIntialConfig);
            });

            return(hostBuilder);
        }
コード例 #5
0
        /// <summary>
        /// This method will create an initialize a generic Host Builder
        /// </summary>
        /// <typeparam name="TApp">Main application type. Used to access user secrets</typeparam>
        /// <param name="args">Application arguments</param>
        /// <param name="localServiceConfiguration">Delegate to be executed to add any non-standard configuration needs</param>
        /// <returns>Configured IHostBuilder</returns>
        public static IHostBuilder CreateHostBuilder <TApp>(string[] args, ConfigureLocalServices <TApp> localServiceConfiguration = null) where TApp : class
        {
            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args)
                                       .ConfigureAppConfiguration((hostingContext, builder) =>
            {
                Assembly CurrentAssembly = typeof(TApp).GetTypeInfo().Assembly;

                // Set up configuration to read appsettings.json and override with secrets.json
                builder.AddUserSecrets(CurrentAssembly);

                // Bind the configuration properties to the properties in the SettingsConfig object
                IConfiguration initialConfig = builder.Build();

                IConfigurationSection myInitialConfig = initialConfig.GetSection(InitialConfigurationSectionName);
                _appSetupConfig = new InitialConfiguration();
                myInitialConfig.Bind(_appSetupConfig);

                if (!string.IsNullOrEmpty(_appSetupConfig.KeyVaultName) && !string.IsNullOrEmpty(_appSetupConfig.KeyVaultKey))
                {
                    // Use the environment variable "InitialConfiguration:RTE" instead of the value in the configuration file
                    // if the environment value is available.
                    // Substitute the runtime environment name in the keyvault properties
                    _appSetupConfig.KeyVaultName = _appSetupConfig.KeyVaultName.Replace("{RTE}", _appSetupConfig.RTE);
                    _appSetupConfig.KeyVaultKey  = _appSetupConfig.KeyVaultKey.Replace("{RTE}", _appSetupConfig.RTE);

                    builder.AddAzureKeyVaultClient(_appSetupConfig.KeyVaultName);
                }

                // Build the final configuration
                _baseConfiguration = builder.Build();

                // Get all the secrets from KeyVault
                _secrets = _baseConfiguration.InitializeApplicationSecrets(_appSetupConfig);

                // Use the KeyVault secrets connect to redis cache
                _cache = _baseConfiguration.InitializeRedisCache(_secrets);

                // Set up automated refresh from redis cache. "TimedCacheRefresh" configuration
                // setting determines which keys are read from the cache and how often they are read.
                // These values are then placed as regular values that can be read from IConfiguration
                _cache?.RefreshConfigurationFromCache(_secrets, _baseConfiguration);
            })
                                       .ConfigureServices((hostingContext, services) =>
            {
                localServiceConfiguration?.Invoke(hostingContext, services, _appSetupConfig);

                services
                .AddTransient <TApp>()
                .AddSingleton <IApplicationSetupConfiguration>(sp =>
                {
                    return(_appSetupConfig);
                })
                .AddSingleton <IApplicationSecrets>(sp =>
                {
                    return(_secrets);
                })
                .AddSingleton <IRedisCache>(sp =>
                {
                    return(_cache);
                })
                .AddSingleton <IHostEnvironment>(sp =>
                {
                    return(hostingContext.HostingEnvironment);
                });

                _serviceProvider = services.BuildServiceProvider();
            })
                                       .ConfigureLogging((hostingContext, logging) =>
            {
                ConfigureCustomLogging(hostingContext, logging, _secrets, _appSetupConfig);
            });

            return(hostBuilder);
        }