Configuration.GetSection always returns null
up vote
11
down vote
favorite
Every time I call Configuration.GetSection
, the Value
property of the returned object is always null.
My Startup
constructor
public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
My ConfigureServices
method
public void ConfigureServices(IServiceCollection services)
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
services.AddOptions();
services.AddMvc();
My appsettings.json
"SqliteSettings":
"DataSource": "C:\db.sqlite",
"NewDatabase": true,
"Version": 3
The class I'm using to define SqliteSettings
public class SqliteSettings
public string DataSource get; set;
public bool? NewDatabase get; set;
public int? Version get; set;
public string Password get; set;
public long? CacheSize get; set;
// More properties
I was thinking the JSON might need to have the same amount of properties to match, or is it might be something to do with data type definitions, but maybe those are completely unrelated.
asp.net-core .net-core
add a comment |
up vote
11
down vote
favorite
Every time I call Configuration.GetSection
, the Value
property of the returned object is always null.
My Startup
constructor
public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
My ConfigureServices
method
public void ConfigureServices(IServiceCollection services)
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
services.AddOptions();
services.AddMvc();
My appsettings.json
"SqliteSettings":
"DataSource": "C:\db.sqlite",
"NewDatabase": true,
"Version": 3
The class I'm using to define SqliteSettings
public class SqliteSettings
public string DataSource get; set;
public bool? NewDatabase get; set;
public int? Version get; set;
public string Password get; set;
public long? CacheSize get; set;
// More properties
I was thinking the JSON might need to have the same amount of properties to match, or is it might be something to do with data type definitions, but maybe those are completely unrelated.
asp.net-core .net-core
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
Every time I call Configuration.GetSection
, the Value
property of the returned object is always null.
My Startup
constructor
public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
My ConfigureServices
method
public void ConfigureServices(IServiceCollection services)
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
services.AddOptions();
services.AddMvc();
My appsettings.json
"SqliteSettings":
"DataSource": "C:\db.sqlite",
"NewDatabase": true,
"Version": 3
The class I'm using to define SqliteSettings
public class SqliteSettings
public string DataSource get; set;
public bool? NewDatabase get; set;
public int? Version get; set;
public string Password get; set;
public long? CacheSize get; set;
// More properties
I was thinking the JSON might need to have the same amount of properties to match, or is it might be something to do with data type definitions, but maybe those are completely unrelated.
asp.net-core .net-core
Every time I call Configuration.GetSection
, the Value
property of the returned object is always null.
My Startup
constructor
public Startup(IHostingEnvironment env)
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
My ConfigureServices
method
public void ConfigureServices(IServiceCollection services)
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
services.AddOptions();
services.AddMvc();
My appsettings.json
"SqliteSettings":
"DataSource": "C:\db.sqlite",
"NewDatabase": true,
"Version": 3
The class I'm using to define SqliteSettings
public class SqliteSettings
public string DataSource get; set;
public bool? NewDatabase get; set;
public int? Version get; set;
public string Password get; set;
public long? CacheSize get; set;
// More properties
I was thinking the JSON might need to have the same amount of properties to match, or is it might be something to do with data type definitions, but maybe those are completely unrelated.
asp.net-core .net-core
asp.net-core .net-core
asked Sep 2 '17 at 19:48
Liam Mueller
168213
168213
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
6
down vote
accepted
Just modify your ConfigureServices
method to be like following
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
And it should work.
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.Configuration.GetSection
still returns with.Value
being null.
– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling.Value
?
– Ali
Sep 2 '17 at 20:07
1
To see if I was getting the JSON, was simply doingvar x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically forValue
.
– Liam Mueller
Sep 2 '17 at 20:10
3
If you want to read directly fromConfiguration
, use something likeConfiguration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern
– Ali
Sep 2 '17 at 20:18
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
|
show 2 more comments
up vote
0
down vote
This worked for me in console application
var conncetionString = config["ConnectionStrings:DefaultConnection"]
add a comment |
up vote
0
down vote
var serviceProvider = services.BuildServiceProvider();
Must come after all services.Configure calls.
add a comment |
up vote
0
down vote
In my case for Web Api Core 2.1 I needed it in the project root folder. Same folder as Startup.cs, Program.cs.
add a comment |
up vote
0
down vote
If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are in both appsettings.Development.json and appsettings.json files.
add a comment |
up vote
-2
down vote
I had this problem before but my problem was that i put my json
config file in wrong place ...
i should have put it in wwwroot
folder . because asp.net core looks for files in this folder
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Just modify your ConfigureServices
method to be like following
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
And it should work.
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.Configuration.GetSection
still returns with.Value
being null.
– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling.Value
?
– Ali
Sep 2 '17 at 20:07
1
To see if I was getting the JSON, was simply doingvar x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically forValue
.
– Liam Mueller
Sep 2 '17 at 20:10
3
If you want to read directly fromConfiguration
, use something likeConfiguration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern
– Ali
Sep 2 '17 at 20:18
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
|
show 2 more comments
up vote
6
down vote
accepted
Just modify your ConfigureServices
method to be like following
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
And it should work.
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.Configuration.GetSection
still returns with.Value
being null.
– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling.Value
?
– Ali
Sep 2 '17 at 20:07
1
To see if I was getting the JSON, was simply doingvar x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically forValue
.
– Liam Mueller
Sep 2 '17 at 20:10
3
If you want to read directly fromConfiguration
, use something likeConfiguration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern
– Ali
Sep 2 '17 at 20:18
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
|
show 2 more comments
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Just modify your ConfigureServices
method to be like following
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
And it should work.
Just modify your ConfigureServices
method to be like following
public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
And it should work.
answered Sep 2 '17 at 20:02
Ali
663513
663513
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.Configuration.GetSection
still returns with.Value
being null.
– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling.Value
?
– Ali
Sep 2 '17 at 20:07
1
To see if I was getting the JSON, was simply doingvar x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically forValue
.
– Liam Mueller
Sep 2 '17 at 20:10
3
If you want to read directly fromConfiguration
, use something likeConfiguration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern
– Ali
Sep 2 '17 at 20:18
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
|
show 2 more comments
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.Configuration.GetSection
still returns with.Value
being null.
– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling.Value
?
– Ali
Sep 2 '17 at 20:07
1
To see if I was getting the JSON, was simply doingvar x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically forValue
.
– Liam Mueller
Sep 2 '17 at 20:10
3
If you want to read directly fromConfiguration
, use something likeConfiguration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern
– Ali
Sep 2 '17 at 20:18
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
2
2
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.
Configuration.GetSection
still returns with .Value
being null.– Liam Mueller
Sep 2 '17 at 20:06
Unforunately, did not work, although the order placement probably solves bugs that would occur in the future.
Configuration.GetSection
still returns with .Value
being null.– Liam Mueller
Sep 2 '17 at 20:06
Could you share how you are calling
.Value
?– Ali
Sep 2 '17 at 20:07
Could you share how you are calling
.Value
?– Ali
Sep 2 '17 at 20:07
1
1
To see if I was getting the JSON, was simply doing
var x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically for Value
.– Liam Mueller
Sep 2 '17 at 20:10
To see if I was getting the JSON, was simply doing
var x = Configuration.GetSection("SqliteSettings");
then inspecting x in Visual Studio, specifically for Value
.– Liam Mueller
Sep 2 '17 at 20:10
3
3
If you want to read directly from
Configuration
, use something like Configuration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern– Ali
Sep 2 '17 at 20:18
If you want to read directly from
Configuration
, use something like Configuration["SqliteSettings:DataSource"];
. And if you want to use it in controllers use options pattern– Ali
Sep 2 '17 at 20:18
1
1
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
I understood that in Startup.cs you can only get configuration json's "leaves" unlike in .net core 1 or so, and use such code: services_.Configure<CorporateConfiguration>( options_ => options_.Name = Configuration.GetValue<string>("Corporate:Name"); options_.BrandLogo = Configuration.GetValue<string>("Corporate:BrandLogo"); );
– barbara.post
Nov 6 '17 at 21:16
|
show 2 more comments
up vote
0
down vote
This worked for me in console application
var conncetionString = config["ConnectionStrings:DefaultConnection"]
add a comment |
up vote
0
down vote
This worked for me in console application
var conncetionString = config["ConnectionStrings:DefaultConnection"]
add a comment |
up vote
0
down vote
up vote
0
down vote
This worked for me in console application
var conncetionString = config["ConnectionStrings:DefaultConnection"]
This worked for me in console application
var conncetionString = config["ConnectionStrings:DefaultConnection"]
answered Jan 31 at 17:08
Sameh
392
392
add a comment |
add a comment |
up vote
0
down vote
var serviceProvider = services.BuildServiceProvider();
Must come after all services.Configure calls.
add a comment |
up vote
0
down vote
var serviceProvider = services.BuildServiceProvider();
Must come after all services.Configure calls.
add a comment |
up vote
0
down vote
up vote
0
down vote
var serviceProvider = services.BuildServiceProvider();
Must come after all services.Configure calls.
var serviceProvider = services.BuildServiceProvider();
Must come after all services.Configure calls.
answered Jul 19 at 8:26
Amorphis
309212
309212
add a comment |
add a comment |
up vote
0
down vote
In my case for Web Api Core 2.1 I needed it in the project root folder. Same folder as Startup.cs, Program.cs.
add a comment |
up vote
0
down vote
In my case for Web Api Core 2.1 I needed it in the project root folder. Same folder as Startup.cs, Program.cs.
add a comment |
up vote
0
down vote
up vote
0
down vote
In my case for Web Api Core 2.1 I needed it in the project root folder. Same folder as Startup.cs, Program.cs.
In my case for Web Api Core 2.1 I needed it in the project root folder. Same folder as Startup.cs, Program.cs.
answered Sep 4 at 20:23
Eric Schneider
3,98523149
3,98523149
add a comment |
add a comment |
up vote
0
down vote
If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are in both appsettings.Development.json and appsettings.json files.
add a comment |
up vote
0
down vote
If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are in both appsettings.Development.json and appsettings.json files.
add a comment |
up vote
0
down vote
up vote
0
down vote
If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are in both appsettings.Development.json and appsettings.json files.
If you have an appsettings.Development.json file, make sure the "ConnectionStrings:DefaultConnection" settings are in both appsettings.Development.json and appsettings.json files.
answered Nov 10 at 15:13
fortBarthadam
513
513
add a comment |
add a comment |
up vote
-2
down vote
I had this problem before but my problem was that i put my json
config file in wrong place ...
i should have put it in wwwroot
folder . because asp.net core looks for files in this folder
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
add a comment |
up vote
-2
down vote
I had this problem before but my problem was that i put my json
config file in wrong place ...
i should have put it in wwwroot
folder . because asp.net core looks for files in this folder
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
add a comment |
up vote
-2
down vote
up vote
-2
down vote
I had this problem before but my problem was that i put my json
config file in wrong place ...
i should have put it in wwwroot
folder . because asp.net core looks for files in this folder
I had this problem before but my problem was that i put my json
config file in wrong place ...
i should have put it in wwwroot
folder . because asp.net core looks for files in this folder
answered May 28 at 12:16
Abolfazl
456410
456410
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
add a comment |
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
3
3
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
No - you should never put it in wwwroot. It goes in your /bin folder. Content in wwwroot could be accidently served from the server, leaking your credentials/config/secrets if any exist in there.
– Johnathon Sullinger
Jul 12 at 19:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46017593%2fconfiguration-getsection-always-returns-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown