ASP.NET Core 1.1 runs fine locally but when publishing to Azure says "An error occurred while starting the application."

32,517

Solution 1

Thanks everyone for your suggestions. The only thing that worked in the end though is deleting that Azure web app that I couldn't publish to, and creating a brand new one. I guess maybe some of the .dlls from the previous runtime environment were still hanging around or not being updated... Whatever it was, re-creating it worked. Hopefully I don't get this error again though, because you can't really do this kind of stuff in production.

Making changes to the global.json file seemed to have no effect.

Creating an entirely new API from a template didn't help either, the issue was with the Azure Web App itself, as everything was running fine locally.

Another very helpful tip was to add logging (and the "logs" file in the root) as per the other answer. That at least pointed me in the right direction. Also checking your runtime with dotnet --version.

Again thanks for everyone's help!

Solution 2

Since many different problems can cause this error page, I can strongly recommend the following in order to determine the root cause quickly and easily, without wrestling Azure (or any server/platform for that matter) to get logs.

You can enable extremely helpful developer friendly error messages at startup by setting the .UseSetting("detailedErrors", "true") and .CaptureStartupErrors(true) actions in your Program.cs file.

For ASP.NET Core 1.x

public static void Main(string[] args)
{
  var host = new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseSetting("detailedErrors", "true")
      .UseIISIntegration()
      .UseStartup<Startup>()
      .CaptureStartupErrors(true)
      .Build();

  host.Run();
}

(2018/07) Update for ASP.NET Core 2.1

public class Program  
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .CaptureStartupErrors(true)
            .UseSetting("detailedErrors", "true")
            .UseStartup<Startup>()
            .Build();
}
  • These settings should be removed as soon as your troubleshooting is complete so as not to expose your application to malicious attacks.

Solution 3

Connect via an sftp client and delete everything in the site/wwwroot folder manually. Republish

I have had nothing but problems since I migrated an application I have hosted on Azure to .net core from MVC 4.

At one point a few weeks ago I was unable to get a project to run after a successful publish. I even tried twice to delete the entire App Service profile and recreate it with the same name. However when I appended a '2' to the App Service name (to create a never before used app service) publishing the exact same project with 0 changes worked perfectly. What exactly does a delete do if I can publish successfully to a new app service but not a deleted and recreated one? Remove Existing Files At Destination was checked in each publish, that didn't do anything either.

I had the same error today as pictured in the OP in my #2 site. It occurred after attempting to update a number of asp nuget packages and re-deploy. Really not wanting to have to move on to iteration myApp3 of my app service, I decided to use the FTP information provided in the azure overview page. I navigated to Site/wwwroot and deleted everything inside from the FTP client. I then published the application, and it worked. I can only conclude that the 'Delete' checkbox doesn't work properly.

Solution 4

I've got the same problem. Just not deployed at Azure, I'm using my local machine as server and host it in IIS.

An error occurred while starting the application.

.NET Core X64 v4.1.1.0    |   Microsoft.AspNetCore.Hosting version 1.1.1    |    Microsoft Windows 10.0.14393    |   Need help?

And this was solved by changing web.config.

First set stdoutLogEnabled = "true"

Then make sure stdoutLogFile=".\logs\stdout" /> this folder exists.

And then restart IIS, you can find the real problem in log file.

Solution 5

DELETE all existing dll from wwwroot/your_application_folder, then copy all of the publish output files&folders.

The problem occurs when the NUGETS update it self. If you don't clean the existing files under wwwroot/your_application_folder IIS gives the error above.

Share:
32,517
derf26
Author by

derf26

Updated on July 14, 2020

Comments

  • derf26
    derf26 almost 4 years

    I've been developing an ASP.NET Core web app, based largely on the MVC template provided in Visual Studio 2017 RC2. It runs just fine in local debug mode, but when I try to publish it to an Azure hosted web app, I get this error:

    enter image description here

    An error occurred while starting the application.

    .NET Core X86 v4.1.1.0 | Microsoft.AspNetCore.Hosting version 1.1.0-rtm-22752 | Microsoft Windows 6.2.9200

    I've tried setting stdoutLogEnabled="true" in the web.config file, but it seems to have no effect, the error is the same.

    Update:

    With some help I managed to retrieve the log, and it says:

    Application startup exception: System.TypeLoadException: Could not load type 'System.IO.File' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.    
       at Microsoft.Extensions.DependencyModel.FileWrapper.OpenRead(String path)
       at Microsoft.Extensions.DependencyModel.DependencyContextLoader.LoadEntryAssemblyContext(IDependencyContextReader reader)
       at Microsoft.Extensions.DependencyModel.DependencyContextLoader.Load(Assembly assembly)    
       at Microsoft.Extensions.DependencyModel.DependencyContext.Load(Assembly assembly)    
       at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.DiscoverAssemblyParts(String entryPointAssemblyName)    
       at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services)    
       at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)    
       at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services)    
       at Bla.Api.Startup.ConfigureServices(IServiceCollection services) in C:\Users\user\Source\Workspaces\Bla\Bla.Api\src\Bla.Api\Startup.cs:line 73
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
       at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
       at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
    
    Hosting environment: Production    
    Content root path: D:\home\site\wwwroot    
    Now listening on: http://localhost:1264    
    Application started. Press Ctrl+C to shut down.
    

    The line of code it refers to at line 73 is:

    services.AddMvc();
    

    Update:

    My global.json file looks like this (where Bla.Api is the name of the project, and the file sits in the solution root folder).

    {
      "projects": [ "Bla.Api" ],
      "sdk": {
        "version": "1.1.0"
      }
    }
    
  • derf26
    derf26 about 7 years
    Thanks for the tip! I've created the folder structure as per the linked answer, and it looks like a log is created, but I can't actually open it. See here: imgur.com/a/bvAD7
  • derf26
    derf26 about 7 years
    It turns out it's because that log file was still open by the application I guess... must have had a handle on it. Re-deploying created a new log file and released the old one, I've updated my question with what the log says.
  • derf26
    derf26 about 7 years
    Thanks for the tip Pete, I'm hoping that if I ever have this issue again, I'll be able to do the same. Like you, I really don't want to be deleting and re-creating web apps!
  • derf26
    derf26 about 7 years
    In my case, the real problem was actually a bug with .NET Core not deploying correctly, not something I had done. Re-creating the entire Azure Web App was the only thing that fixed it, with my code never having been changed. But yes, generally that's good advice to figure out what's happening.
  • Farlop
    Farlop almost 7 years
    This really helped me to find out where my problem was! Thank you so much!
  • rollsch
    rollsch over 6 years
    You can also do this via the developer console in azure.
  • Piotr Kula
    Piotr Kula over 6 years
    Holy cow.. I never knew about that! It is great but is there any way to set it per Environment? :D
  • Steve Land
    Steve Land over 6 years
    @ppumkin yes - just capture settings from your relevant config/environment and use those when calling .UseSetting("detailedErrors", [env-setting]) and .CaptureStartupErrors([env-setting]) See docs.microsoft.com/en-us/aspnet/core/fundamentals/configurat‌​ion
  • Met-u
    Met-u about 6 years
    @Steveland83 How exactly are you supposed to capture the environment? I looked at the link but not seeing exactly which way to go about this. Should I be doing var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")‌​;
  • Steve Land
    Steve Land about 6 years
    @Met-u Yes, exactly that. Dont forget that you will need to manually set these variables for each environment either in your Web.config (see docs.microsoft.com/en-us/aspnet/core/host-and-deploy/… ) or manually on the server as per: stackoverflow.com/a/41551429/7840778
  • Fredrik Lundin
    Fredrik Lundin about 6 years
    This helped me figure out my issue, thank you! For me, the actual issue was a connection string with a missing escape of a `` character..
  • niico
    niico almost 6 years
    I do this - no log file is created