I am trying to accessing the appsettings.json file in the Main method of the Program class as follows:
public static void Main(string[] args)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
string connectionString = configuration.GetConnectionString("DefaultConnection");
}
It works fine without InProcess hosting model but does not work in InProcess model and throws the following error:
The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\Program Files\IIS Express\appsettings.json'.
Now my questions are:
1) Why Directory.GetCurrentDirectory() doesn't work in InProcess hosting model?
2) What is the alternative solution to this to access the appsettings.json when using InProcess hosting model.
Thank you!
@TanvirArjel see https://github.com/aspnet/AspNetCore/issues/4206. workaround https://github.com/aspnet/AspNetCore/issues/4206#issuecomment-443859337
@campersau I have seen issue #4206 before posting this issue. Unfortunately no logical solution found from the answers of that issue.
Our recommendation to work around this is to use the following: https://github.com/aspnet/Docs/blob/master/aspnetcore/host-and-deploy/aspnet-core-module/samples_snapshot/2.x/CurrentDirectoryHelpers.cs. We will be fixing this behavior in an eventual patch.
@jkotalik Please tell me how shall I use this in place of Directory.GetCurrentDirectory() in the above code. Thank you.
Copy the linked class to your project and see https://github.com/aspnet/AspNetCore/issues/4206#issuecomment-445612167
@jkotalik Please tell me how shall I use this in place of
Directory.GetCurrentDirectory()in the above code. Thank you.
I changed to:
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
@leandro-almeida Not working as expected! Throwing the following exception:
The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\GitHub\LoggingWithSerilogInAspNetCore\LoggingWithSerilogInAspNetCore\bin\Debug\netcoreapp2.2appsettings.json'.
@TanvirArjel have you looked if appsettings.json is in the path from the exception-message?
It won't be there, because nobody copied it there :wink:
You can copy it to this location (CopyToOutputDirectoy in properties) or step in the path two level upwards, or use the "official workaround" linked from above.
@gfoidl I have understood the error message. but my question is why isn't not automatically copied to bin folder accurate directory on build?
Because nothing tells MsBuild to do so. appsettings.json has "Copy to Output Directory" set to "Do not copy" by default. You can change this if you want to "Copy if newer", the it will be copied to the build-outputpath.
During publishing the paths are fixed up as expected. But you are running from inside VS (F5)?
@gfoidl Thanks for your explanation.
Seems like questions are resolved here. Closing.
Hi @jkotalik
Is
hostingContext.HostingEnvironment.ContentRootPath more appropriate here rather than: Directory.GetCurrentDirectory()?
Or is this a new feature in the patch which you are referring to?
Most helpful comment
I changed to: