本文整理汇总了C#中Microsoft.Extensions.Configuration.ConfigurationBuilder.Build方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationBuilder.Build方法的具体用法?C# ConfigurationBuilder.Build怎么用?C# ConfigurationBuilder.Build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Extensions.Configuration.ConfigurationBuilder
的用法示例。
在下文中一共展示了ConfigurationBuilder.Build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureConfigServerClientSettingsOptions_WithDefaults
public void ConfigureConfigServerClientSettingsOptions_WithDefaults()
{
// Arrange
var services = new ServiceCollection().AddOptions();
var environment = new HostingEnvironment();
// Act and Assert
var builder = new ConfigurationBuilder().AddConfigServer(environment);
var config = builder.Build();
services.Configure<ConfigServerClientSettingsOptions>(config);
var service = services.BuildServiceProvider().GetService<IOptions<ConfigServerClientSettingsOptions>>();
Assert.NotNull(service);
var options = service.Value;
Assert.NotNull(options);
ConfigServerTestHelpers.VerifyDefaults(options.Settings);
Assert.Equal(ConfigServerClientSettings.DEFAULT_PROVIDER_ENABLED, options.Enabled);
Assert.Equal(ConfigServerClientSettings.DEFAULT_FAILFAST, options.FailFast);
Assert.Equal(ConfigServerClientSettings.DEFAULT_URI, options.Uri);
Assert.Equal(ConfigServerClientSettings.DEFAULT_ENVIRONMENT, options.Environment);
Assert.Equal(ConfigServerClientSettings.DEFAULT_CERTIFICATE_VALIDATION, options.ValidateCertificates);
Assert.Null(options.Name);
Assert.Null(options.Label);
Assert.Null(options.Username);
Assert.Null(options.Password);
}
示例2: GetEnabledTenants
//<inheritdoc/>
public IEnumerable<Tenant> GetEnabledTenants()
{
var fileProvider = _hostingEnvironment.ContentRootFileProvider;
var tenantFolders = fileProvider
.GetDirectoryContents("tenants")
.Where(info => info.IsDirectory);
foreach (var tenantFolder in tenantFolders)
{
var tenantFileName = "tenant.json";
var tenantDirectoryPath = Path.Combine("tenants", tenantFolder.Name);
var tenantFilePath = Path.Combine(tenantDirectoryPath, tenantFileName);
var tenantFileInfo = fileProvider.GetFileInfo(tenantFilePath);
if (tenantFileInfo.Exists)
{
var builder = new ConfigurationBuilder();
builder.SetFileProvider(fileProvider);
builder.AddJsonFile(tenantFilePath);
var config = builder.Build();
var tenant = new Tenant
{
TenantId = tenantFolder.Name,
Configuration = config,
Identities = config.GetSection("Identities").GetChildren().Select(t => t.Value),
Extensions = config.GetSection("Extensions").GetChildren().Select(t => t.Value),
Path = tenantFolder.PhysicalPath
};
yield return tenant;
}
}
}
示例3: Main
public static void Main(string[] args) {
var configBuilder = new ConfigurationBuilder().AddCommandLine(args);
Configuration = configBuilder.Build();
string configFile = Configuration["config"];
if (configFile != null) {
configBuilder.AddJsonFile(configFile, optional: false);
Configuration = configBuilder.Build();
}
ConfigurationBinder.Bind(Configuration.GetSection("startup"), _startupOptions);
ConfigurationBinder.Bind(Configuration.GetSection("security"), _securityOptions);
_loggerFactory
.AddDebug()
.AddConsole(LogLevel.Trace)
.AddProvider(new FileLoggerProvider(_startupOptions.Name));
_logger = _loggerFactory.CreateLogger<Program>();
if (_startupOptions.Name != null) {
_logger.LogInformation(Resources.Info_BrokerName, _startupOptions.Name);
}
var tlsConfig = new TlsConfiguration(_logger, _securityOptions);
var httpsOptions = tlsConfig.GetHttpsOptions(Configuration);
Uri uri = GetServerUri(Configuration);
try {
CreateWebHost(httpsOptions).Run();
} catch(AggregateException ex) when (ex.IsPortInUseException()) {
_logger.LogError(0, ex.InnerException, Resources.Error_ConfiguredPortNotAvailable, uri?.Port);
}
}
示例4: Startup
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
Configuration = builder.Build();
ApplicationBasePath = env.ContentRootPath;
}
示例5: Main
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
builder.AddEnvironmentVariables();
var config = builder.Build();
builder.AddEntityFramework(options => options.UseSqlServer(config["Data:DefaultConnection:ConnectionString"]));
config = builder.Build();
Console.WriteLine("key1={0}", config["key1"]);
Console.WriteLine("key2={0}", config["key2"]);
Console.WriteLine("key3={0}", config["key3"]);
}
示例6: Startup
public Startup(IHostingEnvironment env)
{
// Ugly patch to allow create startup class on tests
if (env != null)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
// TODO: override configuration depending on environment (Windows of Linux)
builder.AddEnvironmentVariables();
LogginConfiguration = builder.Build().GetSection("Logging");
RemoteMediaConfiguration = builder.Build().Get<RemoteMediaConfiguration>();
}
}
示例7: Run
public void Run()
{
try
{
Console.WriteLine("Reading Configuration");
var builder = new ConfigurationBuilder()
.SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
_config = builder.Build();
_ctx = new OldWilderContext(_config);
_newCtx = new WilderContext(_config);
_repo = new WilderRepository(_config, _newCtx);
Console.WriteLine("Migrating Stories");
MigrateStories();
//TODO More migration
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
示例8: Startup
// ReSharper disable once UnusedParameter.Local
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables("Dorothy:");
Configuration = builder.Build();
}
示例9: Startup
public Startup()
{
var builder = new ConfigurationBuilder() // You can write a AddXmlFile extension method yourself.
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
示例10: Main
public static void Main(string[] args = null)
{
ConfigurationBuilder configurationBuilder =
new ConfigurationBuilder();
if (args == null)
{
// Add defaultConfigurationStrings
configurationBuilder.AddInMemoryCollection(
DefaultConfigurationStrings);
}
else
{
configurationBuilder
.AddInMemoryCollection(DefaultConfigurationStrings)
.AddJsonFile("Config.json",
true) // bool indicates file is optional
// "EssentialDotNetConfiguration" is an optional prefix for all
// environment configuration keys
.AddEnvironmentVariables("EssentialDotNetConfiguration")
.AddCommandLine(
args, GetSwitchMappings(DefaultConfigurationStrings));
}
Configuration = configurationBuilder.Build();
Console.WriteLine($"Hello {Configuration["Profile:UserName"]}");
ConsoleWindow consoleWindow =
Configuration.Get<ConsoleWindow>("AppConfiguration:MainWindow");
ConsoleWindow.SetConsoleWindow(consoleWindow);
}
示例11: Startup
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This reads the configuration keys from the secret store.
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
// this file name is ignored by gitignore
// so you can create it and use on your local dev machine
// remember last config source added wins if it has the same settings
builder.AddJsonFile("appsettings.local.overrides.json", optional: true);
// most common use of environment variables would be in azure hosting
// since it is added last anything in env vars would trump the same setting in previous config sources
// so no risk of messing up settings if deploying a new version to azure
builder.AddEnvironmentVariables();
Configuration = builder.Build();
//env.MapPath
appBasePath = appEnv.ApplicationBasePath;
}
示例12: Startup
public Startup(
IHostingEnvironment env,
IApplicationEnvironment appEnv)
{
_env = env;
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
// standard config file
.AddJsonFile("config.json")
// environment specific config.<environment>.json file
.AddJsonFile($"config.{env.EnvironmentName}.json", true /* override if exists */)
// standard Windows environment variables
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// this one adds not using directive to keep it secret, only a dnx reference
// the focus is not on Secrets but on User, so these are User specific settings
// we can also make it available only for developers
builder.AddUserSecrets();
}
Configuration = builder.Build();
}
示例13: Startup
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json");
Configuration = builder.Build();
}
示例14: ConfigureConfiguration
/// <summary>
/// Creates and configures the application configuration, where key value pair settings are stored. See
/// http://docs.asp.net/en/latest/fundamentals/configuration.html
/// http://weblog.west-wind.com/posts/2015/Jun/03/Strongly-typed-AppSettings-Configuration-in-ASPNET-5
/// </summary>
/// <param name="hostingEnvironment">The environment the application is running under. This can be Development,
/// Staging or Production by default.</param>
/// <returns>A collection of key value pair settings.</returns>
private static IConfiguration ConfigureConfiguration(IHostingEnvironment hostingEnvironment)
{
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
// Add configuration from the config.json file.
configurationBuilder.AddJsonFile("config.json");
// Add configuration from an optional config.development.json, config.staging.json or
// config.production.json file, depending on the environment. These settings override the ones in the
// config.json file.
configurationBuilder.AddJsonFile($"config.{hostingEnvironment.EnvironmentName}.json", optional: true);
// This reads the configuration keys from the secret store. This allows you to store connection strings
// and other sensitive settings, so you don't have to check them into your source control provider. See
// http://go.microsoft.com/fwlink/?LinkID=532709 and
// http://docs.asp.net/en/latest/security/app-secrets.html
configurationBuilder.AddUserSecrets();
// Add configuration specific to the Development, Staging or Production environments. This config can
// be stored on the machine being deployed to or if you are using Azure, in the cloud. These settings
// override the ones in all of the above config files.
// Note: To set environment variables for debugging navigate to:
// Project Properties -> Debug Tab -> Environment Variables
// Note: To get environment variables for the machine use the following command in PowerShell:
// $env:[VARIABLE_NAME]
// Note: To set environment variables for the machine use the following command in PowerShell:
// $env:[VARIABLE_NAME]="[VARIABLE_VALUE]"
// Note: Environment variables use a colon separator e.g. You can override the site title by creating a
// variable named AppSettings:SiteTitle. See
// http://docs.asp.net/en/latest/security/app-secrets.html
configurationBuilder.AddEnvironmentVariables();
return configurationBuilder.Build();
}
示例15: Main
public static void Main(string[] args)
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json");
var configuration = configBuilder.Build();
var hostBuilder = new WebHostBuilder();
// set urls and environment
hostBuilder
.UseUrls(configuration["urls"])
.UseEnvironment(configuration["environment"]);
// set other common things
hostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
var host = hostBuilder.Build();
host.Run();
}