简体   繁体   English

ASP.NET Core 1.1 在本地运行良好,但在发布到 Azure 时显示“启动应用程序时出错”。

[英]ASP.NET Core 1.1 runs fine locally but when publishing to Azure says “An error occurred while starting the application.”

I've been developing an ASP.NET Core web app, based largely on the MVC template provided in Visual Studio 2017 RC2.我一直在开发 ASP.NET Core Web 应用程序,主要基于 Visual Studio 2017 RC2 中提供的 MVC 模板。 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:它在本地调试模式下运行得很好,但是当我尝试将它发布到 Azure 托管的 Web 应用程序时,出现此错误:

在此处输入图片说明

An error occurred while starting the application.启动应用程序时发生错误。

.NET Core X86 v4.1.1.0 | .NET Core X86 v4.1.1.0 | Microsoft.AspNetCore.Hosting version 1.1.0-rtm-22752 | Microsoft.AspNetCore.Hosting 版本 1.1.0-rtm-22752 | Microsoft Windows 6.2.9200微软视窗 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.我试过在web.config文件中设置stdoutLogEnabled="true" ,但是好像没有效果,错误是一样的。

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:它在第 73 行引用的代码行是:

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).我的 global.json 文件如下所示(其中 Bla.Api 是项目名称,该文件位于解决方案根文件夹中)。

{
  "projects": [ "Bla.Api" ],
  "sdk": {
    "version": "1.1.0"
  }
}

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.由于许多不同的问题都可能导致此错误页面,因此我强烈推荐以下方法,以便快速轻松地确定根本原因,而无需费力使用 Azure(或任何与此相关的服务器/平台)来获取日志。

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.通过在 Program.cs 文件中设置.UseSetting("detailedErrors", "true").CaptureStartupErrors(true)操作,您可以在启动时启用非常有用的开发人员友好错误消息

For ASP.NET Core 1.x对于 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 (2018/07) 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.完成故障排除后应立即删除这些设置,以免您的应用程序受到恶意攻击。

Connect via an sftp client and delete everything in the site/wwwroot folder manually.通过 sftp 客户端连接并手动删除 site/wwwroot 文件夹中的所有内容。 Republish重新发布

I have had nothing but problems since I migrated an application I have hosted on Azure to .net core from MVC 4.自从我将托管在 Azure 上的应用程序从 MVC 4 迁移到 .net 核心后,我遇到了一些问题。

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.但是,当我将“2”附加到应用服务名称(以创建一个从未使用过的应用服务)时,发布具有 0 个更改的完全相同的项目工作得很好。 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.我今天遇到了与我的 #2 站点的 OP 中的图片相同的错误。 It occurred after attempting to update a number of asp nuget packages and re-deploy.它发生在尝试更新多个 asp nuget 包并重新部署之后。 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.真的不想继续迭代我的应用程序服务的 myApp3,我决定使用 azure 概述页面中提供的 FTP 信息。 I navigated to Site/wwwroot and deleted everything inside from the FTP client.我导航到 Site/wwwroot 并从 FTP 客户端中删除了里面的所有内容。 I then published the application, and it worked.然后我发布了该应用程序,它奏效了。 I can only conclude that the 'Delete' checkbox doesn't work properly.我只能得出结论,“删除”复选框不能正常工作。

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.最后唯一有效的方法是删除我无法发布到的 Azure Web 应用程序,并创建一个全新的应用程序。 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.我想也许之前运行时环境中的一些 .dll 仍然存在或没有更新......不管是什么,重新创建它都有效。 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.对 global.json 文件进行更改似乎没有任何效果。

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.从模板创建全新的 API 也无济于事,问题出在 Azure Web 应用程序本身,因为一切都在本地运行良好。

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 .还使用dotnet --version检查您的运行时。

Again thanks for everyone's help!再次感谢大家的帮助!

I've got the same problem.我有同样的问题。 Just not deployed at Azure, I'm using my local machine as server and host it in IIS.只是没有部署在 Azure,我使用我的本地机器作为服务器并将它托管在 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.这是通过更改 web.config 解决的。

First set stdoutLogEnabled = "true"首先设置stdoutLogEnabled = "true"

Then make sure stdoutLogFile=".\\logs\\stdout" /> this folder exists.然后确保stdoutLogFile=".\\logs\\stdout" />这个文件夹存在。

And then restart IIS, you can find the real problem in log file.然后重新启动 IIS,您可以在日志文件中找到真正的问题。

DELETE all existing dll from wwwroot/your_application_folder, then copy all of the publish output files&folders.从 wwwroot/your_application_folder删除所有现有的 dll,然后复制所有发布的输出文件和文件夹。

The problem occurs when the NUGETS update it self.当 NUGETS 自行更新时会出现问题。 If you don't clean the existing files under wwwroot/your_application_folder IIS gives the error above.如果您不清理 wwwroot/your_application_folder 下的现有文件,IIS 会出现上述错误。

清理和重建修复了一切。

Question is probably duplicated - please refer to ASP.NET Core hosting - 500 internal server error .问题可能重复 - 请参阅ASP.NET Core 托管 - 500 内部服务器错误


Quick answer:快速回答:

You need to set: stdoutLogEnabled="true" and stdoutLogFile=".\\logs\\stdout" .您需要设置: stdoutLogEnabled="true"stdoutLogFile=".\\logs\\stdout" Also, you need to create logs folder manually.此外,您需要手动创建logs文件夹。

In my case, it was because I was trying to publish user secrets for use with Fabook OAuth.就我而言,这是因为我试图发布用于 Fabook OAuth 的用户机密。 I know that's a very situational specific answer, but OAuth seems pretty common these day.我知道这是一个非常具体的答案,但如今 OAuth 似乎很常见。 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.所以为了测试这一点,我临时更改了 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.然后它起作用了。

In My case, that was because I was trying to get a some data in Startup, and dbcontext was not updated in production environment.就我而言,那是因为我试图在 Startup 中获取一些数据,而 dbcontext 未在生产环境中更新。

Changed my ConnectionString to Production and runned Update-Database, and problem solved.将我的 ConnectionString 更改为 Production 并运行 Update-Database,问题解决了。

就我而言,app 目录中缺少一个名为 Resources 的目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure WebApp Asp.NET Core 2 错误:启动应用程序时出错 - Azure WebApp Asp.NET Core 2 error: An error occurred while starting the application ASP.NET Core 2.0 Web 应用程序在将项目部署到 Azure Web 服务时失败并出现 SQLite 错误,但在本地环境中运行良好 - ASP.NET Core 2.0 Web Application fails with a SQLite error when deploying the project to Azure Web Service but runs fine in local environment 启动应用程序时发生错误,一旦我在 IIS 上运行我的 ASP.NET 核心项目 - An error occurred while starting the application, once I run my ASP.NET Core project on IIS .NET 5 启动应用程序时出错。 目录未找到异常 - .NET 5 An error occurred while starting the application. DirectoryNotFoundException ASP.NET 5 启动应用程序时出错 - ASP.NET 5 An error occurred while starting the application 在 SmarterAsp.net WebApplication Asp.NET Core 2.2 中托管时“错误:启动应用程序时发生错误” - " Error: An error occurred while starting the application " while hosting in SmarterAsp.net WebApplication Asp.NET Core 2.2 ASP.net MVC应用程序中的“指定强制转换无效”错误。 在本地工作,并在发布后一段时间,但然后抛出错误 - 'Specified cast is not valid' error in ASP.net MVC application. Works locally, and for a time after publishing, but then throws the error 发布 dot net core 2 应用程序后启动应用程序时发生错误 - an error occurred while starting the application after publishing dot net core 2 app Azure WebApp无法启动:启动应用程序时出错。 加载配置文件失败 - Azure WebApp not starting: An error occurred while starting the application. Loading profile failed 从Visual Studio 2017发布API应用程序后收到错误消息“启动应用程序时发生错误。” - Getting an error message “An error occurred while starting the application.” after publishing an API app from Visual Studio 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM