简体   繁体   中英

async jquery call to async mvc controller

How do i get my jquery methods to call my mvc controller and my mvc controller to do 2 things at the same time?

The jquery code is doint fine. It just calls the methods and keep on going as i want it to.

 $(document).ready(function () {
    console.log("1");
    getADsad();
    console.log("2");
    lala();
    console.log("3");
});
    function getADsad() {
        $.ajax({
            url: '/Configurator/Configure/Hello1',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                console.log(data + "hello1");
            }
        });
    }

function lala() {
    $.ajax({
        url: '/Configurator/Configure/Hello2',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            console.log(data + "hello2");
        }
    });

My C# code on the other hand is not doing two things at a time:

    [HttpPost]
    public async Task<LoginViewModel> Hello1()
    {
        var str = await GetSlowstring();
        return str;
    }

    [HttpPost]
    public async Task<LoginViewModel> Hello2()
    {
        var str = await GetSlowstring();
        return str;
    }

    public async Task<LoginViewModel> GetSlowstring()
    {
        await Task.Delay(10000);
        LoginViewModel login = new LoginViewModel();
        login.UserName = "HejsN";
        return await Task.FromResult(login);
    }

The combined call should take just a little more then 10 seconds if it is done correctly but now it takes the double.

Do i need to create a new thread for the calls? Or is this done automatically by the apppool?

EDIT: 在此输入图像描述

Based on image of Chrome console, problem is in SessionState. By default, when application uses SessionState, ASP framework processes requests from single user serially (to prevent session variables from potential corruption).

For cases, when you want to enable parallel processing of requests for single user (and do not need to update session) you can use SessionState attribute on controller.

 [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

More details can be found for example in here: http://www.stefanprodan.com/2012/02/parallel-processing-of-concurrent-ajax-requests-in-asp-net-mvc/

You are not calling await in your method GetSlowstring , so it synchronously blocks the thread with Thread.Sleep(10000); . For testing purposes, you can try to replace this line of code with Task.Delay method (see MSDN article for this method ):

public async Task<string> GetSlowstring()
{
    await Task.Delay(10000);
    return "hejsan";
}

在此输入图像描述

How about creating two tasks in the MVC controller?

var t1 = new Task(() => { /*The slow task 1 */ });
var t2 = new Task(() => { /*The slow task 2 */});

Task.WaitAll(t1,t2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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