多重请求导致长时间延迟/锁定

时间:2017-06-27 04:44:53

标签: asp.net-core asp.net-core-mvc asp.net-identity entity-framework-core

我遇到一个问题,当我在请求解决之前多次调用端点时,请求响应时间大量爆发。

一些示例代码。

在控制器中

    [HttpGet]
    //[Authorise("Bearer")]
    [Route("test")]
    public string GetDashboardTest()
    {
        var now = DateTime.Now;
        Logger.Information("GetDashboardDataAsync", "Starting API at {0:hh:mm:ss.fff}", now);
        var result = = "Hello World";
        var done = DateTime.Now;
        var time = decimal.Truncate((done.Ticks - now.Ticks) / 10000);
        Logger.InformationAsync("GetDashboardDataAsync", "Ending API at {0:hh:mm:ss.fff}, duration {1}", done, time);
        return result;
    }

这跑得非常快。往返大约20毫秒

当我在其中添加auth时,我们围绕这段代码解决了这个问题。

    public async Task<Response<bool>> UserRequiresReAuthAsync(string username)
    {
        var response = new Response<bool>();
        // This seems to cause locking when i add logging either side of it.
        var user = await UserManager.FindByNameAsync(username);

        // If i use this instead its alot faster but still causes locking
        //var user = await _identityUserRepo.GetAsync(a => a.NormalizedUserName == username.ToUpper());

        response.Succeeded = true;
        response.Data = user == null || user.ReAuth;

        return response;
    }

获取异步的位置。

    returnedColumnsExpression = returnedColumnsExpression ?? (model => model);

    entitySet
        .AsNoTracking()
        .Where(where)
        .Select(returnedColumnsExpression)
        .FirstOrDefaultAsync()

当我添加此内容并向端点发送垃圾邮件时,请求的总时间将分别为10秒。

有没有人对如何避免它有任何想法。

我正在运行dotnetcore 1.1.1

我尝试过调用非同步并且不添加跟踪。

0 个答案:

没有答案