控制器异步方法的多次调用不会同时完成

时间:2018-08-20 07:58:08

标签: c# asp.net iis async-await

我有以下 Home 控制器:

    [HttpGet]
    public async Task IndexAsync()
    {

        Response.Write($"{Thread.CurrentThread.ManagedThreadId} - {DateTime.Now}  begin <br>");
        var con = new SqlConnection(connectionString);
        await con.OpenAsync();
        Response.Write($"{Thread.CurrentThread.ManagedThreadId} - {DateTime.Now}  connection opened <br>");
        var cmd = con.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Await";
        await cmd.ExecuteNonQueryAsync();
        Response.Write($"{Thread.CurrentThread.ManagedThreadId} - {DateTime.Now}  end <br>");
    }

我在浏览器的两个不同选项卡中同时调用同一方法,并获得以下结果:

tab1

enter image description here

tab2

enter image description here

sql过程Await仅执行以下操作:

WAITFOR DELAY '00:00:5'

会话模式为OFF。 为什么方法不能完成并行?

0 个答案:

没有答案