This repository was archived by the owner on Dec 18, 2018. It is now read-only.
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Add back Get<T>() extension method to bind objects to configuration values or entire sections #532
Closed
Description
I am not sure where I am doing something wrong but I am seeing a weird behaviour in terms of getting parsing the JSON config file.
I would appreciate if someone can have a look and tell me where I am doing something stupid 😞
Repro Steps
git clone git@github.com:tugberkugurlu/AspNetCoreSamples.git
cd AspNetCoreSamples/repros/ConfigSectionNull
git checkout -qf 4101a06ab9ab05ac15ed2824a44ff97c323d371e
dotnet restore
dotnet run
Expected Result
Console output to be equal to below:
'MySecondNode' value is null? False
'RabbitMQ' value is null? False
Actual Result
Console output is equal to below:
'settings' is null
'MySecondNode' value is null? True
'RabbitMQ' value is null? True
dotnet CLI Info
Tugberks-MacBook-Pro:ConfigSectionNull tugberk$ dotnet --info
.NET Command Line Tools (1.0.0-preview2-003121)
Product Information:
Version: 1.0.0-preview2-003121
Commit SHA-1 hash: 1e9d529bc5
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.12
OS Platform: Darwin
RID: osx.10.12-x64
Source
Program.cs
:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.PlatformAbstractions;
namespace rabbitsample
{
public static class Program
{
public static void Main(string[] args)
{
var config = ConfigBuilder.Build();
var settings = config.GetValue<RabbitMQSettings>("RabbitMQ");
if(settings == null)
{
System.Console.WriteLine("'settings' is null");
}
var cildrenNodes = config.GetChildren();
foreach (var item in cildrenNodes)
{
System.Console.WriteLine($"'{item.Key}' value is null? {item.Value == null}");
}
}
}
public class RabbitMQSettings
{
public string Host { get; set; }
}
public static class ConfigBuilder
{
public static IConfiguration Build()
{
return new ConfigurationBuilder()
.SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables("rabbitsample_")
.Build();
}
}
}
project.json
:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"copyToOutput": ["config.json"]
},
"dependencies": {
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.Binder": "1.0.0",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0",
"Microsoft.Extensions.Options": "1.0.0",
"Lorem.DNX.NET": "1.0.40",
"easynetq": "2.0.2-netcore0001"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
config.json
:
{
"RabbitMQ": {
"Host": "localhost"
},
"MySecondNode": {
"Value1": "1",
"Value2": "2"
}
}