Added ability for GetConnectionString to handle typical xml App.config or web.config connection string
This commit is contained in:
Родитель
fffd0c44ce
Коммит
905e609909
|
@ -18,7 +18,18 @@ namespace Microsoft.Extensions.Configuration
|
|||
/// <returns></returns>
|
||||
public static string GetConnectionString(this IConfiguration configuration, string name)
|
||||
{
|
||||
return configuration?.GetSection("ConnectionStrings")?[name];
|
||||
var section = configuration?.GetSection("ConnectionStrings");
|
||||
var result = section?[name];
|
||||
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
section = configuration?.GetSection($"connectionStrings:add:{name}:connectionString");
|
||||
if (section != null)
|
||||
{
|
||||
result = section.Value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -264,6 +264,50 @@ namespace Microsoft.Extensions.Configuration.Test
|
|||
Assert.Equal("MemVal3", memVal3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetAppConfigConnectionStrings()
|
||||
{
|
||||
// Arrange
|
||||
var dic = new Dictionary<string, string>()
|
||||
{
|
||||
{"connectionStrings:add:DefaultConnection:connectionString", "MemVal"},
|
||||
};
|
||||
|
||||
var memConfigSrc = new MemoryConfigurationSource { InitialData = dic };
|
||||
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.Add(memConfigSrc);
|
||||
|
||||
var config = configurationBuilder.Build();
|
||||
|
||||
// Act
|
||||
var memVal = config.GetConnectionString("DefaultConnection");
|
||||
|
||||
// Assert
|
||||
Assert.Equal("MemVal", memVal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetConnectionStringReturnsNullIfDoesntExist()
|
||||
{
|
||||
// Arrange
|
||||
var dic = new Dictionary<string, string>()
|
||||
{};
|
||||
|
||||
var memConfigSrc = new MemoryConfigurationSource { InitialData = dic };
|
||||
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.Add(memConfigSrc);
|
||||
|
||||
var config = configurationBuilder.Build();
|
||||
|
||||
// Act
|
||||
var memVal = config.GetConnectionString("DefaultConnection");
|
||||
|
||||
// Assert
|
||||
Assert.Null(memVal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetConfigurationChildren()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче