.Net Asp.Net MVC Development

App can not start after azure deployment

Some times the apps didn’t work after we deploy it on azure and it shows errors like this:
App cannot be started
.NET Core 4.6.27817.03 X86 v4.0.0.0    |   Microsoft.AspNetCore.Hosting version 2.2.0-rtm-35687    |    Microsoft Windows 10.0.14393 

try the following fixes:

First try and fix the program.cs file

public static void Main(string[] args)
        {
            var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json",
                      optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            // Requires using Microsoft.Extensions.Logging;`
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
            logging.AddEventSourceLogger();
        })
        .UseStartup<Startup>()
        .Build();
            webHost.Run();
            // CreateWebHostBuilder(args).Build().Run(); //This is the only line that you will find in the main function by default. so comment it and use the above code.
        }

Kindly note that such changes to your code will change the configuration files, so check your code for such issues.

Leave a Reply

Your email address will not be published. Required fields are marked *