ASP.NET Core MVC 应用程序请求永远不会完成

Duo*_*Duo 5 c# entity-framework asp.net-core-mvc asp.net-core

我正在尝试在单核 Windows Server 2012 主机上托管 ASP.NET Core MVC 项目。我正在使用 ASP.NET Core MVC 1.0.0 并在带有 .NET Framework 4.6.1 的 IIS 8 上运行该应用程序在最初约 10 分钟内工作正常,然后服务器突然停止响应。

起初我以为是数据库端的死锁,所以我在 DbContext 类中添加了日志记录:

private static long ActiveContextCount = 0;
private ILogger _log;
private Stopwatch _timer = new Stopwatch();
private Guid _contextId = Guid.NewGuid();
public BlogContext(string nameOrConnectionString, ILoggerFactory logger)
        :base(nameOrConnectionString)
{
    _log  = logger.CreateLogger<BlogContext>();
    Database.Log = l =>
    {
        _log.LogInformation(l);
    };
    ActiveContextCount++;
    _timer.Start();
    _log.LogInformation("BlogContext created. GUID:{0}, Active count {1}", _contextId, ActiveContextCount);
}

protected override void Dispose(bool disposing)
{
    ActiveContextCount--;
    _timer.Stop();
    _log?.LogInformation("BlogContext Disposed. GUID:{0}, Live time: {1}ms, Active count {2}", _contextId, _timer.ElapsedMilliseconds, ActiveContextCount);
    base.Dispose(disposing);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Entity Framework 6 并且我已经设置了 DbContext 来限定范围:

services.AddScoped(p => new BlogContext(_dataDbConnectionString, p.GetRequiredService<ILoggerFactory>()));
Run Code Online (Sandbox Code Playgroud)

事实证明,数据库操作一切正常。我在日志中跟踪请求并注意到某些请求的最后一个日志为:

[Information] Executing ViewResult, running view at path "/Views/Blog/Details.cshtml".
Run Code Online (Sandbox Code Playgroud)

并且永远不会完成(即无法在日志中找到处置)。从那时起,ActiveContextCount只会堆积成几百个,整个应用程序挂起并停止响应。

(编辑 08/29)另一个例子,甚至没有执行查询:

[Information] Request starting HTTP/1.1 GET http://myapp/  
[Information] UsersContext created. GUID:bb7743b9-7999-4a01-819f-3d239d34d4d9, Active count 7
[Information] HttpContext.User merged via AutomaticAuthentication from authenticationScheme: "Identity.Application".
[Information] BlogContext created. GUID:8c7505d4-5c87-428e-ad56-12f35f54aadc, Active count 6
[Information] Executing action method "MyApp.Controllers.HomeController.Index (MyApp)" with arguments (["1"]) - ModelState is Valid
[Information] Executing ViewResult, running view at path "/Views/Home/Index.cshtml".
Run Code Online (Sandbox Code Playgroud)

我被困在这一点上。为什么视图结果永远不会完成?这是框架中的错误还是我的应用程序中的错误?即使存在错误,为什么在超时后不只是中止和放弃请求?我试图在 web.config 中指定超时但没有帮助:

<aspNetCore processPath=".\MyApp.exe" arguments="" forwardWindowsAuthToken="false" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" requestTimeout="00:00:40" />
Run Code Online (Sandbox Code Playgroud)

我怎样才能进一步调试这个问题?

Duo*_*Duo 5

所以经过几周的调试,我终于找到了问题所在。问题确实在视图中。我浏览了asp.net core doc并注意到了这一行:

PartialAsync 方法可用于包含异步代码的部分视图(尽管通常不鼓励视图中的代码)。

然后我就去看看Html.Partialon github的实际实现。

var result = htmlHelper.RenderPartialAsync(partialViewName, htmlHelper.ViewData.Model, viewData);
result.GetAwaiter().GetResult();
Run Code Online (Sandbox Code Playgroud)

所以同步部分实际上只是异步版本的阻塞包装器!这是经典的异步等待死锁 (1),因为我@await Component.InvokeAysnc在局部视图中调用(因为没有用于调用视图组件的同步版本)。我已经替换了所有Html.Partial参考await Html.PartialAsync并解决了问题。我真的希望他们能对此有更好的警告...... :(

PS我没有发布超过2个链接的声誉,但为什么......

(1): blog.stephencleary.com/2012/07/dont-block-on-async-code.html