(Async& await)vs(在Web API中没有Async& await)

时间:2018-03-14 06:29:15

标签: c# asynchronous async-await

我是Async的新手,等待使用C#编程。在WebAPI中,我们创建了两个API控制器,一个使用Async并等待编程,而另一个则没有。我们已经使用JMeter进行了负载测试,我们得到了以下结果。

Users          Sync                                         Async
100           No Errors                                    No Errors                             
500           No Errors                                    No Errors                             
750           No Errors                                    Errors - (59.0 %) - 502 Bad Gateway   
763           No Errors                                    Errors                                
764           No Errors                                    Errors                                
765           Errors - (0.13 %) - 502 Bad Gateway          Errors
1000          Errors                                      Errors                                

您能解释/建议哪种方法最好或我们如何进行?

API代码:

GetPersonalDetailsController - Async和await Used

public async Task<IHttpActionResult> GET([FromUri] RequestQueryListDTO objAPIRequest)
{
    DateTime startResponseTime = DateTime.Now;
    Response objResponse = null;
    string strResponse = string.Empty;
    var HeaderType = Request.Content.Headers.ContentType;
    ProductBAL objProductBAL = null;
    try
    {
        if (objAPIRequest != null)
        {
            Task<Response> tskGetProductDetails = Task<Response>.Run(() =>
            {
                objProductBAL = new ProductBAL();
                return objProductBAL.GetProductDetails(objAPIRequest);

                //Business Access Layer Logic calling
            });
            objResponse = await tskGetProductDetails;
        }
        else
        {
            objResponse = new Response();
            objResponse.ReturnCode = -1;
            objResponse.ReturnMessage = "Missing Parameters.";
        }
    }
    catch (Exception ex)
    {
        \\ Exception Logging
    }
    finally
    {
        objProductBAL = null;
    }
    objResponse.ResponseTime = Math.Round((DateTime.Now - startResponseTime).TotalMilliseconds).ToString();
    if (objResponse.ReturnCode == Convert.ToInt32(General.ReturnCode))
    {
        return Content<Response>(HttpStatusCode.BadRequest, objResponse);
    }
    else
    {
        return Ok(objResponse);
    }
}

=============================================== =========================

GetPDPController - 不使用Async并等待

public IHttpActionResult GET([FromUri] RequestQueryListDTO objAPIRequest)
{
    DateTime startResponseTime = DateTime.Now;
    Response objResponse = null;
    string strResponse = string.Empty;
    var HeaderType = Request.Content.Headers.ContentType;

    try
    {
        if (objAPIRequest != null)
        {
            //Business Access Layer Logic calling
        }
        else
        {
            objResponse = new Response();
            objResponse.ReturnCode = -1;
            objResponse.ReturnMessage = "Missing Parameters.";
        }
    }
    catch (Exception ex)
    {
        // Exception Logging Code
    }
    finally
    {
        objProductBAL = null;
    }
    objResponse.ResponseTime = Math.Round((DateTime.Now - startResponseTime).TotalMilliseconds).ToString();
    if (objResponse.ReturnCode == Convert.ToInt32(General.ReturnCode))
    {
        return Content<Response>(HttpStatusCode.BadRequest, objResponse);
    }
    else
    {
        return Ok(objResponse);
    }
}

1 个答案:

答案 0 :(得分:0)

我的建议是有两种方法,一种是异步,另一种是非。这样你可以测试更多。

  • GetProductDetails
  • GetProductDetailsAsync

然后您需要更改调用方法的签名,即GET

public IHttpActionResult GET([FromUri] RequestQueryListDTO objAPIRequest)
{
    var objResponse = new Response();

    //check the properties of objAPIRequest
    if(bad)
    {
        //add stuff if you want
        return Content<Response>(HttpStatusCode.BadRequest, objResponse);
    }

    //Business Access Layer Logic calling
    //-----------------------
    ProductBAL objProductBAL = new ProductBAL();

    //you need to change this to async
    var productDetails = objProductBAL.GetProductDetails(objAPIRequest);
    //-----------------------

    return Ok(objResponse);
}