Why `Directory.GetCurrentDirectory()` doesn't work in `InProcess` hosting model
See original GitHub issueI 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:
- Why
Directory.GetCurrentDirectory()
doesn’t work inInProcess
hosting model? - What is the alternative solution to this to access the
appsettings.json
when usingInProcess
hosting model.
Thank you!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:13 (11 by maintainers)
Top Results From Across the Web
Directory.GetCurrentDirectory() doesn't return the correct ...
Right-click a file, then click Properties . Directory. GetCurrentDirectory() can change as a program runs - it should not be used to find...
Read more >Hosting An ASP.NET Core Web Application In IIS
In .NET Core 2.2, a new hosting model was introduced called “In Process”. Instead of IIS forwarding the requests on to Kestrel, it...
Read more >AspNetCoreHostingModel inprocess vs outofprocess
var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseModularStartup()
Read more >In-process hosting with IIS and ASP.NET Core
Learn about the directory structure of published ASP.NET Core apps. Learn how to host an ASP.NET Core app on an IIS server. Understand...
Read more >Notes from changing AspNet Core 2.1-preview1 app to IIS ...
The reason is that when hosting inside w3wp GetCurrentDirectory will return C:\windows\system32\inetsrv while the app is in the C:\inetpub\ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I changed to:
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?