Reputation: 3519
I've created a static class to use my configurations, but when I try to add the JSON file to the configuration I got an exception:
MyConfigurations.json:
{ "ConnectionString": "my connection string", ...}
My static class constructor:
static MyConfigurations() {
var configuration = new Configuration()
.AddJsonFile("MyConfigurations.json")
.AddEnvironmentVariables();
...
...
My exception occurs when the .AddJsonFile
is executed.
Exception: Object reference not set to an instance of an object."
StackTrace:
at Microsoft.Framework.ConfigurationModel.PathResolver.get_ApplicationBaseDirectory() at Microsoft.Framework.ConfigurationModel.JsonConfigurationExtension.AddJsonFile(IConfigurationSourceRoot configuration, String path, Boolean optional) at Microsoft.Framework.ConfigurationModel.JsonConfigurationExtension.AddJsonFile(IConfigurationSourceRoot configuration, String path) at Project.SharedKernel.MyConfigurations..cctor() in C:\Project\Project.SharedKernel\MyConfigurations.cs:line 86
Upvotes: 0
Views: 146
Reputation: 46631
You have not set the application base path which the configuration API needs to determine where the physical config files live. You can set it using the SetBasePath
extension method. A typical implementation looks like this:
public Startup(IApplicationEnvironment appEnv)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("MyConfigurations.json")
.AddEnvironmentVariables()
.Build();
}
Note: this only counts for beta8, see this question. You don't have to specify the default base path anymore in RC1: https://github.com/aspnet/Announcements/issues/88.
Upvotes: 2