dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

About .net core appsettings #27322

Closed ghost closed 3 years ago

ghost commented 3 years ago

Describe the bug

I cant get the appsettings data if I set program startup. Every Time it start, its null. But when i start by myself--after end the thread and manual start the program it works i dont know why. p.s. It also works when i dont set it startup then manual start;

To Reproduce

i dont know how to describe it .

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddRazorPages();

            services.AddOptions();
            services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
        }

 public class LoginController : ControllerBase
    {
        private IOptions<AppSettings> _settings;

        public LoginController(IOptions<AppSettings> settings)
        {
            _settings = settings;
            PubConstant.ConnectionString = settings.Value.ConnectionString;
            LogHelper.WriteLine(JsonConvert.SerializeObject(settings.Value));  
        }
    }

//2020-10-29T10:17:18.0038088+08:00 {"ConnectionString":null,"WashMachine":0,"ServerPort":0} 

appsettings.json
{
  "AppSettings": {
    "ConnectionString": "Server=192.168.0.189;Database=et;Uid=xf;Pwd=1;",
    "WashMachine": 49369,
    "ServerPort": 62102
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Exceptions (if any)

no exceptions

Further technical details

.net core 3.1 windows 10_2004 OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.403\

Host (useful for support): Version: 3.1.9 Commit: 774fc3d6a9

.NET Core SDKs installed: 3.1.403 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Tratcher commented 3 years ago

What does manual start mean here?

ghost commented 3 years ago

What does manual start mean here?

just double click ; i release as WinExe

Tratcher commented 3 years ago

Do you have an appsettings.development.json file in your project? What's in there? That can take priority over appsettings.json when run in VS.

ghost commented 3 years ago

When i run in VS it always works correctly. It break down in release ver when startup with Windows--I mean add it Run in regedit; In project ,i dont have an appsettings.development.json. After release there is one in folder. If i start by meself it works correctly, It can read the appsettings.json. If it start by regedit (Windows) ,program cant read the data in appsettings.json

ghost commented 3 years ago

Do you have an appsettings.development.json file in your project? What's in there? That can take priority over appsettings.json when run in VS.

https://github.com/AldousS22/appsettingsTestDemo/tree/master/appsettingsTestDemo You can have a try .I print the text to event viewer.

Tratcher commented 3 years ago

Oh, that make sense. ASP.NET relies on the current working directory to find the appsettings.json file. I bet when windows starts the exe it uses a different current directory (maybe c:\windows\system32 like it does for windows services?).

You can log the Environment.CurrentDirectory at startup to confirm this theory. You should also see a log about the Content Root folder. That's where the app looks for the appsettings.json. You can override the location using a command line switch contentroot. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#contentroot-1

ghost commented 3 years ago

Oh, that make sense. ASP.NET relies on the current working directory to find the appsettings.json file. I bet when windows starts the exe it uses a different current directory (maybe c:\windows\system32 like it does for windows services?).

You can log the Environment.CurrentDirectory at startup to confirm this theory. You should also see a log about the Content Root folder. That's where the app looks for the appsettings.json. You can override the location using a command line switch contentroot. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.1#contentroot-1

Thanks. Sry for reply so late. I did it then it works currently. When the program start by regedit, it use the Windows folder not the program folder, so it can find the appsettings. Thanks