Asynchronous API Synchronous Action Methods

I have an action method that inserts some data into a database. I am trying to make it asynchronous. The code is as follows:

public async Task<ActionResult> Create([DataSourceRequest] DataSourceRequest request, MyClass model)
{
    return await Task.Run(() =>
    {
        if (model != null && ModelState.IsValid)
        {
            try
            {
                repository.Insert(model);

                anotherSyncCall(model);
            }
            catch { ModelState.AddModelError("", "Error"); }
        }

        return Json(new[] { model }.ToDataSourceResult(request, ModelState));
    });
}

The repository insert method is synchronous, do I need to make it asynchronous to make this action method asynchronous?

Repository Insertion Code:

private EFDbContext context;
public Repository()
{
    context = new EFDbContext(Authentication.GetConnectionClaim);
}
//......
public void Insert(T entity)
{
    context.Set<T>().Add(entity);
    context.SaveChanges();
}

This is an image of timings for repeated requests.

enter image description here

As you can see, they work synchronously. I am sure that my client side Ajax calls are asynchronous because all requests are executed immediately and they get responses over time.

UPDATE

This is the code I'm testing for my application:

for (var i = 0; i < 100; i++) {
        $.ajax({
            method: 'post',
            url: 'http://192.168.1.8/xx/xx/Read',
            data: 'sort=&page=1&pageSize=10&group=&filter='
        });
    }

And I tried installing SessionStateon Readonly, as @Dan suggested, but id did not change the timings.

enter image description here

, , ToDataSourceResult Kendo, ? Task action method.

+4
2

SessionState ReadOnly? :

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

Microsoft:

ASP.NET , , , , . , ( SessionID), . . ( , , - .) EnableSessionState @ ReadOnly, . , .

+2

Task.Run , . , ( ). , . . , ajax , . async/await , , Stephen Cleary Conference, .

+1

Source: https://habr.com/ru/post/1622670/


All Articles