Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with reading appsettings file using Host.CreateDefaultBuilder #3

Open
valeriob opened this issue Nov 4, 2019 · 4 comments
Open

Comments

@valeriob
Copy link

valeriob commented Nov 4, 2019

Creating a default host builder via this api
var builder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
it does not load appsettings.json settings.

This is due to CreateDefaultBuilder setting the content root to
builder.UseContentRoot(Directory.GetCurrentDirectory());
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0
and at that time GetCurrentDirectory is c:\windows\system32

@stevenvolckaert
Copy link

stevenvolckaert commented Feb 28, 2020

@valeriob According to Host ASP.NET Core in a Windows Service on Microsoft Docs, GetCurrentDirectory() always returns C:\Windows\system32 for Windows Services.

It does state, however, that loading the app's default settings files - appsettings.json and appsettings.{Environment}.json - are loaded from the app's content root by calling Host.CreateDefaultBuilder(args).

Does it make any difference if you call RunAsTopshelfService after CreateDefaultBuilder?

Alternatively, you might workaround this issue by calling:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .RunAsTopshelfService()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("appsettings.json");
            })
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });

@vunb
Copy link

vunb commented Jan 15, 2021

Hi, you can replace Directory.GetCurrentDirectory() by using the path of the proccess:

CreateHostBuilder(args)
  // set base path for application
  .UseContentRoot(Path.GetDirectoryName(Process
    .GetCurrentProcess()
    .MainModule.FileName))
  .RunAsTopshelfService();

@lekrus
Copy link

lekrus commented Jan 28, 2021

The only solution worked is the same as used in UseWindowsService() extension:

var rc = CreateHostBuilder(args)
  .UseContentRoot(AppContext.BaseDirectory)
  .RunAsTopshelfService();

@EsmailOuadghiri
Copy link

The only solution worked is the same as used in UseWindowsService() extension:

var rc = CreateHostBuilder(args)
  .UseContentRoot(AppContext.BaseDirectory)
  .RunAsTopshelfService();

Thank you soooooooooo much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants