Added ability for GetConnectionString to handle typical xml App.config or web.config connection string

This commit is contained in:
Devon Burriss 2016-07-27 23:54:57 +02:00 коммит произвёл d-burriss
Родитель fffd0c44ce
Коммит 905e609909
2 изменённых файлов: 56 добавлений и 1 удалений

Просмотреть файл

@ -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()
{