本文整理汇总了C#中Microsoft.Framework.ConfigurationModel.Configuration类的典型用法代码示例。如果您正苦于以下问题:C# Configuration类的具体用法?C# Configuration怎么用?C# Configuration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Configuration类属于Microsoft.Framework.ConfigurationModel命名空间,在下文中一共展示了Configuration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddCaching();
services.AddSession();
services.AddMvc();
services.AddSingleton<PassThroughAttribute>();
services.AddSingleton<UserNameService>();
services.AddTransient<ITestService, TestService>();
services.ConfigureMvc(options =>
{
options.Filters.Add(typeof(PassThroughAttribute), order: 17);
options.AddXmlDataContractSerializerFormatter();
options.Filters.Add(new FormatFilterAttribute());
});
#if DNX451
// Fully-qualify configuration path to avoid issues in functional tests. Just "config.json" would be fine
// but Configuration uses CallContextServiceLocator.Locator.ServiceProvider to get IApplicationEnvironment.
// Functional tests update that service but not in the static provider.
var applicationEnvironment = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");
// Set up configuration sources.
var configuration = new Configuration()
.AddJsonFile(configurationPath)
.AddEnvironmentVariables();
string diSystem;
if (configuration.TryGet("DependencyInjection", out diSystem) &&
diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
{
_autoFac = true;
services.ConfigureRazorViewEngine(options =>
{
var expander = new LanguageViewLocationExpander(
context => context.HttpContext.Request.Query["language"]);
options.ViewLocationExpanders.Insert(0, expander);
});
// Create the autofac container
var builder = new ContainerBuilder();
// Create the container and use the default application services as a fallback
AutofacRegistration.Populate(
builder,
services);
builder.RegisterModule<MonitoringModule>();
var container = builder.Build();
return container.Resolve<IServiceProvider>();
}
else
#endif
{
return services.BuildServiceProvider();
}
}
示例2: RegisterConfiguration
public static IConfigurationSourceRoot RegisterConfiguration([NotNull] this Startup startup, [NotNull] string toolsDirectory, [NotNull] string projectDirectory, ConfigurationOptions options)
{
var configuration = new Microsoft.Framework.ConfigurationModel.Configuration();
configuration.Add(new MemoryConfigurationSource());
configuration.Set(Constants.Configuration.ToolsDirectory, toolsDirectory);
configuration.Set(Constants.Configuration.ProjectDirectory, projectDirectory);
configuration.Set(Constants.Configuration.SystemConfigFileName, "scconfig.json");
var configurationService = new ConfigurationService(configuration);
if ((options & ConfigurationOptions.DoNotLoadConfig) != ConfigurationOptions.DoNotLoadConfig)
{
try
{
configurationService.Load(options);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return null;
}
}
return configuration;
}
示例3: Configure
public void Configure(IBuilder app)
{
// Setup configuration sources
var configuration = new Configuration();
configuration.AddEnvironmentVariables();
app.UseOwin();
// Set up application services
app.UseServices(services =>
{ // Add MVC services to the services container
services.AddMvc();
services.SetupOptions<MvcOptions>(options => {
System.Diagnostics.Debug.WriteLine(options.OutputFormatters.Select(item => item.GetType().Name));
options.OutputFormatters.RemoveAt(0);
});
});
// Add static files to the request pipeline
app.UseStaticFiles();
// Add MVC to the request pipeline
app.UseMvc(routes =>
{
//routes.MapRoute(
// name: "default",
// template: "{controller}/{action}/{id?}",
// defaults: new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "api",
template: "{controller}/{id?}");
});
}
示例4: CanGetSubKey
public void CanGetSubKey()
{
// Arrange
var dic1 = new Dictionary<string, string>()
{
{"Data:DB1:Connection1", "MemVal1"},
{"Data:DB1:Connection2", "MemVal2"}
};
var dic2 = new Dictionary<string, string>()
{
{"DataSource:DB2:Connection", "MemVal3"}
};
var dic3 = new Dictionary<string, string>()
{
{"Data", "MemVal4"}
};
var memConfigSrc1 = new MemoryConfigurationSource(dic1);
var memConfigSrc2 = new MemoryConfigurationSource(dic2);
var memConfigSrc3 = new MemoryConfigurationSource(dic3);
var config = new Configuration();
config.AddLoadedSource(memConfigSrc1);
config.AddLoadedSource(memConfigSrc2);
config.AddLoadedSource(memConfigSrc3);
string memVal1, memVal2, memVal3, memVal4, memVal5;
bool memRet1, memRet2, memRet3, memRet4, memRet5;
// Act
var configFocus = config.GetSubKey("Data");
memRet1 = configFocus.TryGet("DB1:Connection1", out memVal1);
memRet2 = configFocus.TryGet("DB1:Connection2", out memVal2);
memRet3 = configFocus.TryGet("DB2:Connection", out memVal3);
memRet4 = configFocus.TryGet("Source:DB2:Connection", out memVal4);
memRet5 = configFocus.TryGet(null, out memVal5);
// Assert
Assert.True(memRet1);
Assert.True(memRet2);
Assert.False(memRet3);
Assert.False(memRet4);
Assert.True(memRet5);
Assert.Equal("MemVal1", memVal1);
Assert.Equal("MemVal2", memVal2);
Assert.Equal("MemVal4", memVal5);
Assert.Equal("MemVal1", configFocus.Get("DB1:Connection1"));
Assert.Equal("MemVal2", configFocus.Get("DB1:Connection2"));
Assert.Null(configFocus.Get("DB2:Connection"));
Assert.Null(configFocus.Get("Source:DB2:Connection"));
Assert.Equal("MemVal4", configFocus.Get(null));
Assert.Equal("MemVal1", configFocus["DB1:Connection1"]);
Assert.Equal("MemVal2", configFocus["DB1:Connection2"]);
Assert.Null(configFocus["DB2:Connection"]);
Assert.Null(configFocus["Source:DB2:Connection"]);
Assert.Equal("MemVal4", configFocus[null]);
}
示例5: Settings
internal Settings(string projectDirectory, string settingsFile)
{
var configuration = new Configuration();
configuration.AddJsonFile(Path.Combine(projectDirectory, settingsFile));
_configuration = configuration;
}
示例6: Can_use_connection_string_name_in_OnConfiguring_test
private async Task Can_use_connection_string_name_in_OnConfiguring_test(string connectionName)
{
var configuration = new Configuration
{
new MemoryConfigurationSource(
new Dictionary<string, string>
{
{ "Data:Northwind:ConnectionString", SqlServerTestDatabase.NorthwindConnectionString }
})
};
var serviceCollection = new ServiceCollection();
serviceCollection
.AddInstance<IConfiguration>(configuration)
.AddEntityFramework()
.AddSqlServer();
var serviceProvider = serviceCollection.BuildServiceProvider();
using (await SqlServerTestDatabase.Northwind())
{
using (var context = new NorthwindContext(serviceProvider, connectionName))
{
Assert.Equal(91, await context.Customers.CountAsync());
}
}
}
示例7: Main
public Task<int> Main(string[] args)
{
//Add command line configuration source to read command line parameters.
var config = new Configuration();
config.AddCommandLine(args);
var serviceCollection = new ServiceCollection();
serviceCollection.Add(HostingServices.GetDefaultServices(config));
var services = serviceCollection.BuildServiceProvider(_hostServiceProvider);
var context = new HostingContext()
{
Services = services,
Configuration = config,
ServerName = "Microsoft.AspNet.Server.WebListener",
ApplicationName = "BugTracker"
};
var engine = services.GetService<IHostingEngine>();
if (engine == null)
{
throw new Exception("TODO: IHostingEngine service not available exception");
}
using (engine.Start(context))
{
Console.WriteLine("Started the server..");
Console.WriteLine("Press any key to stop the server");
Console.ReadLine();
}
return Task.FromResult(0);
}
示例8: OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
var config = new Configuration()
.AddJsonFile("config.json");
var constr = config.Get("Data:DefaultConnection:ConnectionString");
options.UseSqlServer(constr);
}
示例9: Startup
/// <summary>
/// Constructor - load the configuration files and store in the
/// static area.
/// </summary>
public Startup()
{
Configuration = new Configuration()
.AddJsonFile("config.json", optional: true)
.AddJsonFile("config-local.json", optional: true)
.AddEnvironmentVariables();
}
示例10: EnvDefaultsToDevelopmentConfigValueIfSpecified
public void EnvDefaultsToDevelopmentConfigValueIfSpecified()
{
var vals = new Dictionary<string, string>
{
{ "ASPNET_ENV", "Staging" }
};
var config = new Configuration()
.Add(new MemoryConfigurationSource(vals));
var context = new HostingContext
{
ServerFactory = this,
Configuration = config
};
var engine = new HostingEngine();
using (engine.Start(context))
{
Assert.Equal("Staging", context.EnvironmentName);
var env = context.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal("Staging", env.EnvironmentName);
}
}
示例11: Startup
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
var configuration = new Configuration();
configuration.AddEnvironmentVariables();
Configuration = configuration;
}
示例12: Startup
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configPath = Path.Combine(appEnv.ApplicationBasePath, "..", "..");
Configuration = new Configuration(configPath).AddJsonFile("config.json").
AddEnvironmentVariables("SmsPrize_");
}
示例13: Startup
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables();
}
示例14: ConfigureServices
/// <summary>
/// Sets up the DI container. Loads types dynamically (http://docs.autofac.org/en/latest/register/scanning.html)
/// </summary>
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
/* TODO: #10
services.AddCaching();
services.AddSession();
services.ConfigureSession(o =>
{
o.IdleTimeout = TimeSpan.FromMinutes(5);
});*/
services.AddTransient<WopiAuthorizationAttribute>();
// Autofac resolution
var builder = new ContainerBuilder();
// Configuration
Configuration configuration = new Configuration();
configuration.AddEnvironmentVariables();
builder.RegisterInstance(configuration).As<IConfiguration>().SingleInstance();
// File provider implementation
var providerAssembly = configuration.Get("WopiFileProviderAssemblyName");
var assembly = AppDomain.CurrentDomain.Load(new AssemblyName(providerAssembly));
builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
builder.Populate(services);
var container = builder.Build();
return container.Resolve<IServiceProvider>();
}
示例15: Configure
public void Configure(IApplicationBuilder app)
{
var config = new Configuration();
config.AddEnvironmentVariables();
config.AddJsonFile("config.json");
config.AddJsonFile("config.dev.json", true);
config.AddUserSecrets();
var password = config.Get("password");
if (config.Get<bool>("RecreateDatabase"))
{
var context = app.ApplicationServices.GetService<Models.BlogDataContext>();
context.Database.EnsureDeleted();
System.Threading.Thread.Sleep(2000);
context.Database.EnsureCreated();
}
if (config.Get<bool>("debug"))
{
app.UseErrorPage();
app.UseRuntimeInfoPage();
}
else
{
app.UseErrorHandler("/home/error");
}
app.UseMvc(routes => routes.MapRoute(
"Default", "{controller=Home}/{action=Index}/{id?}"));
app.UseFileServer();
}