Sunday, December 18, 2016

Seeing detailed error message for ASP.NET Core 1.0 app deployed on Azure App Services

I am sure I am not alone in scratching my head when trying to figure out why an ASP.NET Core 1.0 web app deployed  to Azure crashes and does not work. That happened to me earlier today when I attempted to deploy an ASP.NET Core 1.0 web app that uses the SQLite database to Azure. The error message I received (Oops. 500 Internal Server Error | An error occurred while starting the application) was cryptic and not too helpful – see below:

image

This is the generic error that is displayed when web apps have their environment variable set to production or staging.

This bears the question – how does one set the environment variable in Azure App Services to a setting that sheds some light on what the problem is? Well, Here’s what I determined:
The name of the environment variable key that your ASP.NET Core 1.0 app uses can be determined by looking at your project’s Debug properties in Visual Studio 2015:

image

Alternatively, you can peek into Properties/launchSettings.json to see the values for the environment variable in question:

image

This means that the environment variable that controls detailed error messages is ASPNETCORE_ENVIRONMENT. Ideally, the three common values for this variable are Development, Staging and Production.

My next thought is -, how do I set the ASPNETCORE_ENVIRONMENT environment variable on Azure App Services. It turned out quite easy to do.

1) Login to your Azure subscription then click on “App Services” on the left-side.

image

2) Next, click on your web application. In my case it is SQLiteWeb, as shown below:

image

3) On the next blade, click on “Application Settings”:

image

4) On the next blade, scroll further down until you find the “App Settings” section. Add key=ASPNETCORE_ENVIRONMENT and value=Development as shown below:

image

Don’t forget to click on Save when you are done.

Now refresh the page that had a cryptic error and, voila, you can see a more meaningful error message:

image

Even though the error message looks ugly, at least I have something to work on.
BTW, I was able to solve the error by adding the following statement at the bottom of the Configure() method in Startup.cs:

context.Database.Migrate();

My final Configure() method looks like this:
public void Configure(IApplicationBuilder app,
    IHostingEnvironment env,
    ILoggerFactory loggerFactory,
    SchoolContext context)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
      app.UseDeveloperExceptionPage();
      app.UseBrowserLink();
    }
    else
    {
      app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    app.UseMvc(routes =>
    {
      routes.MapRoute(
          name: "default",
          template: "{controller=Home}/{action=Index}/{id?}");
    });

    context.Database.Migrate();
    DummyData.Initialize(context);
}

References:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments
http://druss.co/2016/01/asp-net-core-1-0-automatic-migrations-entity-framework-7-ef-core-1-0/

No comments:

Post a Comment