简体   繁体   English

Web API中的(Async&await)与(没有Async&await)

[英](Async & await) vs (without Async & await) in Web API

I am new with Async and await using C# Programming. 我是Async的新手,正在等待使用C#编程。 In WebAPI, we have created two API Controllers one with Async and await Programming and other is without that. 在WebAPI中,我们创建了两个API控制器,一个具有Async并等待编程,而另一个则没有。 We have done load testing using JMeter and we have got following results. 我们已经使用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                                

Can you any please explain/suggest which approach is best or how can we proceed ? 能否请您解释/建议哪种方法最好,或者我们如何进行?

API Code : API代码:

GetPersonalDetailsController - Async and await Used GetPersonalDetailsController-异步和等待使用

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 - Without using Async and await GetPDPController-不使用异步和等待

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);
    }
}

My suggestion is have two methods, one Async and one not. 我的建议是有两种方法,一种是异步的,一种不是。 That way you can test more. 这样,您可以进行更多测试。

  • GetProductDetails 获取产品详细信息
  • GetProductDetailsAsync GetProductDetailsAsync

you would then need to change the signature of the calling method aka GET 然后,您需要更改调用方法(即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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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