简体   繁体   English

ASP.NET Core:使用开发环境变量的非常慢(第一)页面加载时间

[英]ASP.NET Core: Very slow (first) page load time with development environment variable

My ASP.NET Core application went very slow on debugging from one day to another. 我的ASP.NET核心应用程序从一天到另一天的调试速度非常慢。 It tooks up to 90 seconds to load a single site, which was loaded in max 5-6 seconds before at the first request. 加载单个站点需要花费90秒,在第一次请求之前最多加载5-6秒。 I didn't made any changes which could explain this. 我没有做任何可以解释这个的改变。 I tried different things to find out why. 我尝试了不同的东西来找出原因。 It seems that the slowdown-process begins with the request. 似乎减速过程始于请求。

I found out that this is only the case when I set the ENVIRONMENT variable in the project options under debugging to Development . 我发现只有当我在调试中的项目选项中将ENVIRONMENT变量设置为Development时才会出现这种情况。 For testing I set it to Production and surprisingly my APP was pretty fast like before this issue. 为了进行测试,我将其设置为Production ,令人惊讶的是我的APP在此问题之前非常快。 I could only find the pre-generated part from the example where the variable is checked: 我只能从检查变量的示例中找到预生成的部分:

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

But although I commented those lines out, my ASP.NET Core application is stil very slow when setting ENVIRONMENT=Development. 但是虽然我对这些行进行了评论,但在设置ENVIRONMENT = Development时,我的ASP.NET核心应用程序非常缓慢。 I have no idea why. 我不知道为什么。 I could only explain this with the theory that ASP.NET Core, EF Core or other parts of the framework check the variable internally and do something which requires much time when in development-environment. 我只能用ASP.NET Core,EF Core或框架的其他部分在内部检查变量并在开发环境中做一些需要很多时间的理论来解释这一点。

For now its a acceptable workaround to use the production environment. 目前,它是使用生产环境的可接受的解决方法。 But on the long run I would like to use this nice variable to do some things only in development like filling the database with some test-data. 但是从长远来看,我想使用这个漂亮的变量来做一些只在开发中做的事情,就像用一些测试数据填充数据库一样。 What can be the reason for this performance problem in dev-mode? 在开发模式下出现这种性能问题的原因是什么?

The output of the webserver says: 网络服务器的输出说:

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

As you can see its not a startup problem, because it tooks most of the time for executing the action. 您可以看到它不是启动问题,因为它花费了大部分时间来执行操作。 But the action of the home-controller only serves a simple Razor view, not at least a database-connection is created there. 但是家庭控制器的动作只提供简单的Razor视图,至少在那里创建了数据库连接。

To see how slow this is, here a example of the same request with ENVIRONMENT=Production 要了解这是多么缓慢,这里有一个与ENVIRONMENT = Production相同的请求示例

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

And the second request, where ASP.NET has most of the things initialized/cached: 第二个请求,其中ASP.NET具有初始化/缓存的大部分内容:

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

I found out that this was not directly related to ASP.NET Core. 我发现这与ASP.NET Core没有直接关系。 Instead, the microsoft symbol-servers were enabled in the debug-settings on options > debugging > symbols: 而是在选项>调试>符号的调试设置中启用了微软符号服务器:

在此输入图像描述

This made Visual Studio to load the symbols of all microsoft assemblys. 这使得Visual Studio可以加载所有Microsoft软件程序集的符号。 It seems that the symbol-cache was not persistent cause it was stored in a temp folder. 似乎符号缓存不是持久的,因为它存储在临时文件夹中。 So VS reload them from time to time, which result in obvious performance issues. 因此VS会不时重新加载它们,这会导致明显的性能问题。 I disabled them and now the development-area is nearly as fast as production. 我禁用它们,现在开发区几乎和生产一样快。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM