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

C#.NetAzureModel View-Controllerasp.net Core

C# Problem Overview


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"
  }
}

C# Solutions


Solution 1 - C#

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 2 - C#

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 3 - C#

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 4 - C#

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 - C#

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.

Solution 6 - C#

Clean and rebuild fixed everything.

Solution 7 - C#

Question is probably duplicated - please refer to ASP.NET Core hosting - 500 internal server error.


Quick answer:

You need to set: stdoutLogEnabled="true" and stdoutLogFile=".\logs\stdout". Also, you need to create logs folder manually.

Solution 8 - C#

In my case, it was because I was trying to publish user secrets for use with Fabook OAuth. I know that's a very situational specific answer, but OAuth seems pretty common these day. User Secrets, it turns out, are not meant to be published. Who knew.

So to test this I temporarily changed the following code in startup.cs. This data should be not hard coded as a part of best practice, as it would end up in clear text in source control.

Before

    app.UseFacebookAuthentication(new FacebookOptions()
    {
        AppId = Configuration["Authentication:Facebook:AppId"],
        AppSecret = Configuration["Authentication:Facebook:AppSecret"]
    });

After

    app.UseFacebookAuthentication(new FacebookOptions()
    {
        AppId = "0000000000000", // your value
        AppSecret = "0000000000000000000000000000000" // your value
    });

Then it worked.

Solution 9 - C#

In My case, that was because I was trying to get a some data in Startup, and dbcontext was not updated in production environment.

Changed my ConnectionString to Production and runned Update-Database, and problem solved.

Solution 10 - C#

In my case there was a directory named Resources that was missing in app directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionderf26View Question on Stackoverflow
Solution 1 - C#Steve LandView Answer on Stackoverflow
Solution 2 - C#PeteView Answer on Stackoverflow
Solution 3 - C#derf26View Answer on Stackoverflow
Solution 4 - C#wtf512View Answer on Stackoverflow
Solution 5 - C#FuatView Answer on Stackoverflow
Solution 6 - C#Alex Jhones SnView Answer on Stackoverflow
Solution 7 - C#Lukasz MkView Answer on Stackoverflow
Solution 8 - C#DaveView Answer on Stackoverflow
Solution 9 - C#mesutView Answer on Stackoverflow
Solution 10 - C#Hamid RezaView Answer on Stackoverflow