Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 371
Chris Avatar asked Jun 25 '19 20:06

Chris


1 Answers

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.

like image 180
Kirk Larkin Avatar answered Nov 14 '22 23:11

Kirk Larkin