c# - ASP.NET 核心 : Very slow (first) page load time with development environment variable

标签 c# visual-studio-2015 asp.net-core asp.net-core-mvc .net-core

我的 ASP.NET Core 应用程序的调试速度一天比一天慢。加载单个站点最多需要 90 秒,在第一次请求之前最多加载 5-6 秒。我没有做任何可以解释这一点的改变。我尝试了不同的方法来找出原因。减速过程似乎是从请求开始的。

我发现,只有当我将调试下的项目选项中的ENVIRONMENT变量设置为Development时,才会出现这种情况。为了进行测试,我将其设置为Production,令人惊讶的是,我的 APP 与此问题之前一样快。我只能从检查变量的示例中找到预生成的部分:

if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            DebugData.InsertIfMissing(); // This is from me and inserts some testdata in the db
        } else {
            app.UseExceptionHandler("/Home/Error");
        }

但是,尽管我注释掉了这些行,但在设置 ENVIRONMENT=Development 时,我的 ASP.NET Core 应用程序仍然非常慢。我不知道为什么。我只能用 ASP.NET Core、EF Core 或框架的其他部分在内部检查变量并在开发环境中做一些需要很多时间的事情来解释这一点。

目前,使用生产环境是一种可接受的解决方法。但从长远来看,我想使用这个很好的变量来做一些只在开发中的事情,比如用一些测试数据填充数据库。开发模式下出现此性能问题的原因可能是什么?

网络服务器的输出显示:

Hosting environment: Development
Content root path: C:\Dev\MyApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Executing action method MyApp.Controllers.HomeController.Index (MyApp) with arguments () - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor[1]
      Executing ViewResult, running view at path /Views/Home/Index.cshtml.
info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
      User profile is available. Using 'C:\Users\MyUserName\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action MyApp.Controllers.HomeController.Index (MyApp) in 84836.6896ms

如您所见,这不是启动问题,因为它花费了大部分时间来执行操作。但是家庭 Controller 的操作只服务于一个简单的 Razor View ,而不是至少在那里创建一个数据库连接。

要了解这有多慢,这里有一个 ENVIRONMENT=Production 相同请求的示例

info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action MyApp.Controllers.HomeController.Index (MyApp) in 2728.3558ms

第二个请求,其中 ASP.NET 已初始化/缓存了大部分内容:

info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
  Executed action MyApp.Controllers.HomeController.Index (MyApp) in 19.3621ms

最佳答案

我发现这与 ASP.NET Core 没有直接关系。相反,在选项 > 调试 > 符号的调试设置中启用了 Microsoft 符号服务器:

enter image description here

这使得 Visual Studio 加载所有 Microsoft 程序集的符号。似乎符号缓存不是持久的,因为它存储在临时文件夹中。所以 VS 时不时地重新加载它们,这导致了明显的性能问题。我禁用了它们,现在开发区几乎和生产区一样快。

关于c# - ASP.NET 核心 : Very slow (first) page load time with development environment variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277386/

相关文章:

c# - .net User Secrets 在 Windows 10 上不起作用

枚举的 C# 显式初始化

c# - .net 4.0 上的 BlockingCollection 非常独特的异常(exception)

c++ - 如何判断 libcurl 是否正确处理了我的 SSL 文件

c# - .NET Core Web API : Possible to assign controller route in appsettings. json 配置文件?

c# - 翻译在 C# 中使用 Active Directory 的 VB

ios - Visual Studio 2015 RTM Cordova IOS 构建错误 - 找不到模块 'Q'

c# - 从 Visual Studio 2015 RC 升级到 Visual Studio 2015 Community RTM 后,Blend 无法工作

c# - 在 asp .net core 3 中使用进程内托管设置请求超时

angular - ASP.NET Core 多 Angular 应用程序