AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Code to clear all the configuration providers breaks my app in Azure App Service

Open sameer-kumar opened this issue 3 years ago • 10 comments

I have used code as described in this article to clear all configuration providers and then add my own providers with order. However, when I deploy this AspNet Core 3.1 app in Azure App Service(Linux), it breaks my application.

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;
                    string[] args = null;

                    // ***If this following line is removed, app loads and behaves as expected****
                    config.Sources.Clear();

                    config
                        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"appsettings.Win2012.json", optional: true, reloadOnChange: true)
                        .AddJsonFile($"appsettings.override.json", optional: true, reloadOnChange: true);

                    // secrets
                    if (env.IsDevelopment())
                    {
                        var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                        if (appAssembly != null)
                        {
                            config.AddUserSecrets(appAssembly, optional: true);
                        }

                        // Try adding secrets (if not added above) from specified assembly.
                        if (!config.Sources.Where(cf =>
                        {
                            if (cf is JsonConfigurationSource jsonconfigurationSource)
                            {
                                return jsonconfigurationSource.Path == "secrets.json";
                            }
                            else
                            {
                                return false;
                            }
                        }).Any())
                        {
                            var appAssembly2 = Assembly.Load(new AssemblyName("VSSP.Web"));
                            if (appAssembly2 != null)
                            {
                                config.AddUserSecrets(appAssembly2, optional: true);
                            }
                        }
                    }

                    config.AddEnvironmentVariables();
                    config.AddEnvironmentVariables(prefix: "App.Web_");
                    config.AddEnvironmentVariables("LIB_");

                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

What am I missing here?

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

sameer-kumar avatar Oct 07 '22 13:10 sameer-kumar

Breaks how? Please provide the simplest possible sample that reproduces the problem on a publicly accessible GitHub repository.

Rick-Anderson avatar Oct 07 '22 21:10 Rick-Anderson

I created a simplest repo here There is nothing fancy, just stock template AspNetCore 3.1 MVC app with modified Program.cs with code referenced from https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1. This works locally however breaks when hosted in Azure App Service (Linux).

sameer-kumar avatar Oct 10 '22 15:10 sameer-kumar

This works locally however breaks when hosted in Azure App Service (Linux).

Expected result?

Actual result?

Rick-Anderson avatar Oct 10 '22 19:10 Rick-Anderson

Expected Result: Application should launch home page properly. image

Actual Result: Application crashes. image

sameer-kumar avatar Oct 12 '22 21:10 sameer-kumar

See https://learn.microsoft.com/en-us/aspnet/core/test/troubleshoot-azure-iis?view=aspnetcore-3.1#troubleshoot-on-azure-app-service

What's the error?

Rick-Anderson avatar Oct 12 '22 22:10 Rick-Anderson

@alexwolfmsft @codemillmatt Create a ASP.NET Core 3.1 empty web app dotnet new web -o myApp -f netcoreapp3.1 set deployment to self contained to Linux Log shows: DotNet Runtime 3.1WARNING: Unable to find the startup DLL name. Could not find any files with extension '.runtimeconfig.json' Per Startup File section set the compiled DLL name as dotnet <myapp>.dll

image

I get the following error It was not possible to find any installed .NET Core SDKs

Rick-Anderson avatar Oct 12 '22 23:10 Rick-Anderson

I have turned on File system logging. However, Log stream options are disabled for me. I guess it doesn't work on ASE. I have looked into Log stream from Kudu but no error in those streams. Also, app runs fine if I don't change anything in stock Program.cs file.

sameer-kumar avatar Oct 13 '22 15:10 sameer-kumar

Try live streaming. Did you deploy self contained?

Rick-Anderson avatar Oct 13 '22 19:10 Rick-Anderson

no it's not self-contained.

sameer-kumar avatar Oct 13 '22 22:10 sameer-kumar

no it's not self-contained.

You need to figure out how to see the error logs.

Rick-Anderson avatar Oct 13 '22 22:10 Rick-Anderson

To add some extra info on this, for any others that may run into the same issue ...

We ran into the same issue as described by @sameer-kumar and found that this led to a mismatch between the exposed port of the container and the port within the container that the .Net core app was listening on.

The docker command that App Service issues starts as docker run -d --expose=8080 --name .. and unless you specify the WEBSITES_PORT setting this is the default port it expects the container to be listening on as well. The execution of the config.Sources.Clear command results in the container potentially listening on a different port (as per the below), and traffic from the app service to the container therefore has nowhere to go.

With config.Sources.Clear : image

Without config.Sources.Clear : image

nabeelp avatar Dec 09 '22 12:12 nabeelp