61

I have upgraded asp.net core 1.1 to an asp.net core 2. It runs fine on the local server, but when I try to deploy it to an Azure hosted web app, I received the error:

An error occurred while starting the application. .NET Core

4.6.00001.0 X86 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.0.0-rtm-26452 | Microsoft Windows 6.2.9200

enter image description here

Any ideas?

Melchia
  • 22,578
  • 22
  • 103
  • 117
Arvind Sisara
  • 861
  • 2
  • 7
  • 13
  • 1
    If you get `startup` errors you can enable extra startup options to output the actual reason. Have a look at my blog post about it. http://pkula.blogspot.co.uk/2017/11/asp-net-core-how-to-fix-error-occurred.html – Piotr Kula Nov 15 '17 at 10:58
  • Possible dublicate: https://stackoverflow.com/questions/43493259/asp-net-core-publish-error-an-error-occurred-while-starting-the-application/51988150 – George Kosmidis Aug 24 '18 at 09:22
  • 1
    @PiotrKula OMG, thanks :) I had issue with create database at startup – Bimal Das Jun 05 '19 at 06:25
  • Glad it helped. They could just do that I the template commented out or so – Piotr Kula Jun 16 '19 at 16:36

7 Answers7

121

Please add ASPNETCORE_DETAILEDERRORS = true in app settings of your app, restart it and see the detailed error next time you load the url. That will help you fix it.

For example, error in my case was that I didn't have the managed identity of my API App configured to access the Key Vault to get the storage account and Cosmos DB keys. I used startup to inject the configured storage and cosmos db objects hence it was failing the moment I was starting my app.

When you've fixed the startup issue, don't forget to remove this setting as leaving it on could expose information about how the application works to visitors in the event of another error.

Captain Obvious
  • 608
  • 5
  • 14
Suneet Nangia
  • 1,670
  • 1
  • 11
  • 7
  • 21
    For reference, go to your Azure Dashboard -> All Resources -> Your App Service -> Application Settings in the left hand sidebar -> scroll down to `Application settings` which is a key:value pair of settings, input the above. Restart your web app and you should have a nice error message displayed. Thanks Suneet, hours of debugging were fixed by this! – James Gould Jan 31 '19 at 11:46
  • Works, but would you want to use this/leave this enabled on a production site? – Andrew May 10 '19 at 12:13
  • @Andrew no definitely do not. Disable this after you resolve the issue. Don't wanna be leaking out troubleshooting details in prod when not needed. – Marchy Jun 08 '19 at 00:27
  • 5
    This worked! Only that it seems the location of the setting was different for me. It was under the app service -> Configuration (in the settings section on the left navigation). Is there a way to have such information logged so I can see it later but not visible in the production site to users? – Michael Tontchev Aug 14 '19 at 15:43
  • Somebody please give these instructions for non azure applications please :/ – Thomas Harris Jul 03 '20 at 12:05
  • @ThomasHarris see https://stackoverflow.com/a/56473932/1484041 – midgetspy Apr 28 '21 at 20:30
  • @JayGould the app will restart automatically after you save the new app setting in the Azure Portal. – David Klempfner Jan 25 '22 at 04:30
7

Got my tips from https://scottsauber.com/2017/04/10/how-to-troubleshoot-an-error-occurred-while-starting-the-application-in-asp-net-core-on-iis/

  1. Open your web.config
  2. Change stdoutLogEnabled=true
  3. Create a logs folder Unfortunately, the AspNetCoreModule doesn’t create the folder for you by default If you forget to create the logs folder, an error will be logged to the Event Viewer that says: Warning: Could not create stdoutLogFile \?\YourPath\logs\stdout_timestamp.log, ErrorCode = -2147024893. The “stdout” part of the value “.\logs\stdout” actually references the filename not the folder. Which is a bit confusing. Run your request again, then open the \logs\stdout_*.log file

Note – you will want to turn this off after you’re done troubleshooting, as it is a performance hit.

So your web.config’s aspNetCore element should look something like this

<aspNetCore processPath=”.\YourProjectName.exe” stdoutLogEnabled=”true” stdoutLogFile=”.\logs\stdout” />
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
NoloMokgosi
  • 1,678
  • 16
  • 10
  • 7
    look something like what? – ViqMontana Jan 16 '19 at 16:20
  • once you enable it in the web.config, how do you tell IIS to log there? It doesn't log any errors at all even after changing the web.config and rebooting the server! Is there another special, hidden, secret setting somewhere? I hate IIS for lack of the complete steps necessary to do even the simplest of things (like logging errors). – MC9000 Jun 23 '20 at 10:50
  • Adding to @NoloMokgosi answer above. Don't forget to give proper permissions to newly created log folders.@MC900 have you given proper permissions? – Ninja Dec 18 '20 at 02:30
6

Enable DetailedErrorsKey in the Program.cs so you can figure it out what's happening.

WebHost.CreateDefaultBuilder(args)
    .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
Masoud Darvishian
  • 3,754
  • 4
  • 33
  • 41
3

You can obtain more details by enabling ASPNETCORE_DETAILEDERRORS = true:

go to your Azure Dashboard -> Your App Service

In your application Settings (in the left hand sidebar) -> scroll down to configuration which is a key:value pair of settings, input the above. Restart your web app.

Melchia
  • 22,578
  • 22
  • 103
  • 117
3

Try executing the application by firing the command

dotnet myapplicationname.dll

This shall throw the startup errors and may help you narrow down the error.

Sunil Johnson
  • 1,059
  • 8
  • 6
  • i'm getting this rows wih the command: C:\inetpub\2>dotnet DiscountPoolBackbone.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: C:\inetpub\2 – Ustin Feb 20 '20 at 18:58
2

I managed to solve the problem myself and I hope this solution might help someone.

First, I have set log folder on the Azure server and find issue with more details. I forgot some database changes in SQL.Update database changes and run it's working fine now.

Arvind Sisara
  • 861
  • 2
  • 7
  • 13
1

I was able to discover my exceptions for this error by starting the app from it's published executable.

To try this locally, you can right click on the web project, click publish, and then publish it to a folder. There should be an executable in the folder of the same name as the project. Run that and it should show exceptions in the console.

farlee2121
  • 2,959
  • 4
  • 29
  • 41