Search code examples
iisasp.net-core-2.0shared-hostingsecret-managerasp.net-core-configuration

hosting .net core app gives 502.5 on using add json to configuration


I am trying to host a .net core application on A2hosting shared hosting windows option, which uses IIS. However, once I used UserSecretsId in the .csproj file for the secret manager, I was getting the 502.5 error. This is odd since production doesn't use the secret manager. After doing some digging I found adding the following line in my .csproj file gets rid of the 502.5 error again:

<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>

Now, I need to store my secrets outside the web folder. I am trying to use the following code in my program.cs to set this up:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseConfiguration(new ConfigurationBuilder()
                        .AddJsonFile("<path here>")   // line causing issue
                        .Build())
        .Build();

This code brings back the error 502.5. When I comment out the AddJsonFile line alone, the error is gone. I was trying to understand what is wrong with AddJsonFile.

In conclusion, the issue seems to revolve around IConfiguration (with secret manager and AddJsonFile).

Any idea how I can get the AddJsonFile function working? Any good alternates to access an external json file? I am trying for something that is not messy like reading a while file and serializing it into AddInMemoryCollection function. Is the a way to log the errors shown as 502.5? I have noticed that with this errors, middlewares don't run. This means I can't use the ILogger in them.


Solution

  • This error was no longer shown when AddJsonFile("<path here>") showed a valid path. I could use the overload AddJsonFile("<path here>", true) which would ignore this function if the file is not found. This way the 502.5 was resolved once the path was valid. It is however still a mystery as to how an invalid path shows a 502.5 error. I am also not sure if this has something to do with the configuration package.