question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why `Directory.GetCurrentDirectory()` doesn't work in `InProcess` hosting model

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
leandro-almeidacommented, Dec 11, 2018

@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();
1reaction
AlexKeySmithcommented, Jan 22, 2019

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found