How to get .Net Core 3.1 to see my appsettings files

2

I created an api using .net core 3.1 and like these things are, it works on my machine. But it also does not work on my machine when I try to publish it to IIS. I have to use IIS as one of my requirements. So the errors are:

  1. HTTP Error 500.30 - ANCM In-Process Start Failure. I tried a bunch of stuff but nothing seemed to have work. I went through the appsettings files to see if I missed a comma or something and I changed the settings in IIS so that the application pool does not managed code and disabled the 32-bit application option.
  2. Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\inetpub\wwwroot\appnamehere\appsettings..json'. I found this in the Event viewer.

The Event Viewer errors are the following:

Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' failed to load coreclr. Exception message: CLR worker thread exited prematurely

Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' hit unexpected managed exception, exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs: Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\inetpub\wwwroot\appnamehere\appsettings..json'. File name: 'C:\inetpub\wwwroot\appnamehere\appsettings..json' at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) at System.IO.File.InternalReadAllText(String path, Encoding encoding) at System.IO.File.ReadAllText(String path) at appnamehere.Services.Configurations.DatabaseConfigProvider.FetchConfiguration() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 25 at appnamehere.Services.Configurations.DatabaseConfigProvider.Load() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 20 at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at appnamehere.Services.Configurations.AppConfiguration.GetCurrentSettings() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 34 at appnamehere.Services.Configurations.AppConfiguration.get_Current() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 24 at appnamehere.Services.Data.Implementation.TitanDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Development\solutionNameHere\appnamehere.Services.Data.Implementation\TitanDbContext.cs:line 17 at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() at appnamehere.Startup..ctor(IConfiguration configuration) in C:\Development\solutionNameHere\appnamehere\Startup.cs:line 27 --- End of stack trace from previous location where exception was thrown --- at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at appnamehere.Program.Main(String[] args) in C:\Development\solutionNameHere\appnamehere\Program.cs:line 20

So I went to that location and there all my appsettings files were both appsettings.json and appsettings.Development.json. Then I went to look at where the error happened.

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var host = CreateHostBuilder(args, env).Build();

            host.Run();
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            NLog.LogManager.Shutdown();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args, string env) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureAppConfiguration((IConfigurationBuilder builder) =>
            {
                builder.Sources.Clear();
                builder.SetBasePath(Directory.GetCurrentDirectory());
                builder.AddDataBaseProvider();
                builder.AddJsonFile($"appsettings.{env}.json");
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
                logging.AddEventSourceLogger();
                logging.AddFile(hostingContext.Configuration.GetSection("Logging"));
            });
}

I tried hard coding builder.AddJsonFile($"appsettings.{env}.json"); to explicitly refer to appsettings.Development.json, but the error persisted.

How do I get the application to see the appsettings.Development.json file after I published? It works perfectly when I run it through Visual Studio.

c#
appsettings
asp.net-core-3.1
asked on Stack Overflow Jan 22, 2020 by spmoolman • edited Jan 22, 2020 by spmoolman

1 Answer

0

I found the issue. The application pool for the site used the default ApplicationPoolIdentity and not my local user. So it did not read the User environmental variables hence it worked in Visual Studio but not through IIS.

My solution was to change

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

to

var env = "name-Of-File"
answered on Stack Overflow Jan 22, 2020 by spmoolman

User contributions licensed under CC BY-SA 3.0