异步MVC.NET操作方法会阻止任何其他HTTP请求 [英] Async MVC.NET action method blocks any other HTTP requests

查看:109
本文介绍了异步MVC.NET操作方法会阻止任何其他HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我很抱歉更改问题)

以下代码片段来自MVC.NET控制器(.NET:v4.5; AspNet.MVC:v5.2.3). 调用 LongOperation 后,它会:

The following snippet is from a MVC.NET controller (.NET: v4.5; AspNet.MVC: v5.2.3) . After LongOperation is called, it:

  • 产生一个进程
  • 等待完成
  • 监视一些LOG文件
  • 使用 SignalR 将日志文件中的进度通知浏览器
  • Spawn a process
  • Waits for its completion
  • Monitors a few LOG files
  • Uses SignalR to notify browser of the progress from the LOG files

(为简单起见,我省略了代码)

(I have omitted the code for simplicity)

所有这些工作正常,只有在 LongOperation 正在运行时,其他 HTTP请求都不会由控制器处理.

All this works, only while LongOperation is running, no other HTTP requests are handled by the controllers.

LongOperation 完成并且操作方法将结果返回给AJAX调用之后,会对其进行处理.

They get handled after the LongOperation completes and the action method returns result to the AJAX call.

我搞砸了什么? 预先谢谢你.

What am I messing up? Thank you in advance.

更新(针对@angelsix评论): 这是一个简化的设置:

Update (for @angelsix comment): Here is a simplified setup:

  • 我已按照建议删除了async/await
  • 根据建议添加了断点
  • 验证它们是否如上述
  • 所述命中

基本上:相同的结果,请参见console.log-ed的文本和时间戳 将感谢社区的任何帮助. 预先谢谢你!

Basically: same result, see the console.log-ed text and timestamps Will appreciate any help from the community. Thank you in advance!

GUI和日志

控制器中的操作方法

[AjaxOnly]
public ActionResult _RunLongOperation(string hubId)
{
    try
    {
        for (int i = 0; i < 10; i++)
        {
            Thread.Sleep(1000);
            ProgressNotifierHub.Notify(hubId, string.Format("Notification from _RunLongOperation {0}", i));
        }
        return new HttpStatusCodeResult(HttpStatusCode.OK, "_RunLongOperation : OK");
    }
    catch (Exception)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "_RunLongOperation : NOK");
    }
}

[AjaxOnly]
public ActionResult _RunAnotherOperation(string hubId)
{
    return new HttpStatusCodeResult(HttpStatusCode.OK, "_RunAnotherOperation : OK");
}

Razor View(部分)和具有SignalR集线器设置Ajax调用的javascript

<script src="~/signalr/hubs"></script> 

@{ 
    Layout = null;
}

<button id="longOperationBtn" type="button" class="t-button" style='width: 155px'>Long Operation</button>
<button id="anotherOperationBtn" type="button" class="t-button" style='width: 155px'>Another Operation</button>


<script type="text/javascript">

    $(function () {
        setupEventHandlers();
        setupProgressNorificator();
    });


    function setupEventHandlers() {

        $('#longOperationBtn').click(function (event) {
            requestOperation('_RunLongOperation')
        });

        $('#anotherOperationBtn').click(function (event) {
            requestOperation('_RunAnotherOperation')
        });
    }

    function requestOperation(method) {
        trace(method + ' requested');

        $.ajax({
            url: '/Profiles/Validate/' + method,
            type: 'GET',
            data: { hubId: $.connection.hub.id },
            contentType: 'application/json; charset=utf-8',
            success: function () {
                trace(method + ' completed');
            },
            error: function () {
                trace(method + ' failed');
            }
        });
    }

    function setupProgressNorificator(profileId) {
        var hub = $.connection.progressNotifierHub;
        hub.client.notify = function (notification) {
            console.log(notification);
        };
        $.connection.hub.start();
    }

    function trace(s) {
        console.log('[' + new Date().toUTCString() + '] ' + s);
    }

</script>

推荐答案

您似乎在Chrome上运行客户端测试".我不确定2018年的限制是什么(似乎在发行版本之间有所变化),但是浏览器确实限制了允许到同一主机的并发连接数.我相信Chrome的上限是6或8.

It looks like you are running the client "test" on Chrome. I'm not sure what the 2018 limitation is (it seems to change from release to release), but browsers do limit the number of concurrent connections allowed to the same host. I believe Chrome's limit is 6 or 8.

您发布的日志似乎来自客户端.如果是这样,您看到的行为实际上可能是客户端在等待免费连接以连接到服务器-问题可能根本不是与ASP.NET有关,而是与您的测试方式有关.

The log you posted appears to be from the client side. If it is, the behavior you are seeing could actually be the client waiting for a free connection to connect to the server - the problem may not be with ASP.NET at all, but rather with how you are testing.

这很容易验证-在另一个浏览器窗口中编写另一个可以与当前测试同时运行的测试,只需调用"short"操作即可.如果有延迟,那我错了.如果没有,希望我能帮忙!

It would be easy to validate - write another test that you can run concurrently with your current test in a separate browser window that just calls the "short" operation. If it has latency, I'm wrong. If it doesn't, well hopefully I've helped out!

这篇关于异步MVC.NET操作方法会阻止任何其他HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆