Self-Contained ASP.Net Core Not Reading appsettings.json file
Asked Answered
H

1

10

I wrote a ASP.Net Core 2.2 application. Everything works fine when I run it on my dev machine.

I have published and deployed it to my staging machine as a self-contained application.

The TargetFramework is netcoreapp2.2. The RuntimeIdentifier is win-x64. And the Environment is Staging.

When running the app through the command line for some testing, it doesn't seem to be reading the appsettings.staging.json or any of the appsettings.json files.

For testing purposes I set the Configure Method of Startup.cs as follows:

public void Configure( IApplicationBuilder app , IHostingEnvironment env )
{
    if( env.IsDevelopment( ) )
    {
        app.UseDeveloperExceptionPage( );
    }
    else
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts( );
    }

    app.UseHttpsRedirection( );

    Console.WriteLine( $"Chris Environment: {env.EnvironmentName}" );
    Console.WriteLine( $"Chris IsStaging: {env.IsStaging( )}" );
    Console.WriteLine( $"Chris ConnectionString: {Configuration.GetConnectionString( "DefaultConnection" )}" );
    Console.WriteLine( $"Chris LoggingAPI: {Configuration["LoggingAPIURL"]}" );

    foreach( var test in Configuration.AsEnumerable( ) )
    {
        var key = test.Key;
        var val = test.Value;
        Console.WriteLine( $"Chris Key: {key} - Value: {val}" );
    }

    app.UseMvc( b =>
    {
        b.Select( ).Expand( ).Filter( ).OrderBy( ).MaxTop( 100 ).Count( );
        b.MapODataServiceRoute( "default" , "api" , EdmModelBuilder.GetEdmModel( app.ApplicationServices ) );
    } );
}

I run the app by typing in the command line: path/To/My/App.exe --environment Staging

The results the write out are: Chris Environment: Staging Chirs IsStaging: True Chris ConnectionString: Chris LoggingAPI:

The connectionstring and the LoggingAPI are left blank. The loop returns a bunch of values, but nothing that is in any of the appsettings.json files.

My appsettings.json file looks like:

{
  "ConnectionStrings": {
    "DefaultConnection": "Some ConnectionString"
  },
 "Logging": {
   "LogLevel": {
     "Default": "Warning"
   }
  },
   "AllowedHosts": "*",
   "LoggingAPIURL": "SomeURL"
}

I have verified that the appsetting.json files are on the server.

Can someone explain to me what is going on?

Hibernate answered 25/6, 2019 at 20:27 Comment(0)
S
22

The base path for WebHostBuilder configuration is set to IHostingEnvironment.ContentRootPath (source):

var builder = new ConfigurationBuilder()            
    .SetBasePath(_hostingEnvironment.ContentRootPath)

When using WebHostBuilder.CreateDefaultBuilder, which is the default approach generated by the project templates, IHostingEvironment.ContentRootPath is set using Directory.GetCurrentDirectory() (source):

builder.UseContentRoot(Directory.GetCurrentDirectory());

This means that, when attempting to locate appsettings.json and appsettings.[Environment].json, the working directory is used and not necessarily the application's directory. In your example, you're running the application from outside of its own directory, which means the .json files are not being found.

To resolve this, you can first cd into path/To/My and then run App.exe from there. Alternatively, if you were to run App.exe as a service, you could set the working directory to be the same directory as the app itself.

Sanction answered 26/6, 2019 at 17:41 Comment(3)
would suggest to promote this golden answer everywhere in like dozen of questions regarding "my iconfiguration section is null" and such..Cellule
Still true is .NET 6. This answer explained why I could not run my app from the command prompt window using an full path, e.g., cmd /c "C:\Outbound\MyAppName\MyAppName.exe" (It was unable to find the appsettings.json file that way.) Based on your answer, when I wrote the command as cd "C:\Outbound\MyAppName" && cmd /c MyAppName.exe it worked perfect.Nerti
Your solution helped me with Linux CentOS and .Net6. Thank you!Barbellate

© 2022 - 2024 — McMap. All rights reserved.