Sync http request in Azure function

o Merry 1 Reputation point
2021-12-28T07:53:50.61+00:00

I am new to Azure functions, because I have to import external library in my Playfab's CloudScript, and this is the way I can do.
So I have some basic question about Azure functions, here is my scene:

  1. My game backend send an ExecuteCloudScript request to Playfab, which calls an Azure function deployed.
  2. This Azure function I called it "CreateOrderNo", and it makes another http request(use external library) which will return an trade order number and an QRcode after about 200ms.
  3. My game backend received this order number and an QRcode, and my game backend begins to query the trade state on this order number through another Azure function called "Notify".
  4. Finally, my game backend received the order state like success or failed, and do something else.

Now I have created these two Azure functions, but I don't know whether it is the best way to code, for example, I remove the await keywork in my Azure function and it warns me that this async function doesn't have await operater, and it will run in sync way.

  1. It seems that this Azure function will be low efficient?
  2. or it cannot handle the second request until my first trade number(first request) returns?
  3. I am not familliar with concurrency, and I thought I should not worry about these in Azure function , for example, I just make a sync http request in my Azure function and it will be managed by Azure or someone else? But it seems not, so I have to know is it right to call http request in Azure function.

here is my code of CreateOrderNo:

public static class CreateOrderNo
    {
        [FunctionName("CreateOrderNo")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a CreateOrderNo.");
            string steamid = req.Query["steamid"];
            string amount = req.Query["amount"];
            string subject = req.Query["subject"];
            try
            {
                // 1. create IAopClient instance
                IAopClient client = new DefaultAopClient(
                    Config.Gatewayurl,
                    Config.AppId, 
                    Config.PrivateKey, 
                    "json", "1.0", "RSA2",
                    Config.AlipayPublicKey,
                    "utf-8",
                    false);


                // 2. create request object
                AlipayTradePrecreateRequest request = GetRequest(null, 1, "abc");
                // 3. execute request and wait for response
                AlipayTradePrecreateResponse response = client.Execute(request);
                // client.Execute(request);
                if (!response.IsError)
                {
                    Console.WriteLine("success。" + response.QrCode);
                }
                else
                {
                    Console.WriteLine("failed:" + response.Msg + "," + response.SubMsg);
                }
                log.LogInformation("response.Body:" + response.Body);
                log.LogInformation("response.OutTradeNo:" + response.OutTradeNo);
                log.LogInformation("response.QrCode:" + response.QrCode);

                var result = new AopDictionary();
                result.Add("out_trade_no", response.OutTradeNo);
                result.Add("qr_code", response.QrCode);

                log.LogInformation("CreateOrderNo over.");
                var json_result = JsonConvert.SerializeObject(result);
                return new OkObjectResult(json_result);
            }
           catch (Exception e)
            {
                Console.WriteLine("call failed:" + e.Message);
                throw e;
            }
}

Thanks for help.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee Moderator
    2022-01-04T17:15:35.44+00:00

    @o Merry TL;DR Use Asynchronous APIs where possible for I/O Operations. In your case, the async version of the client.Execute() is available.

    Here are answers to your queries

    It seems that this Azure function will be low efficient?

    Not exactly but since your code is synchronous, it could lead to thread exhaustion since the network request blocks your code. So, you might not be able to handle the same number of requests for the same compute muscle. It is always recommended to use asynchronous APIs for I/O operations.

    or it cannot handle the second request until my first trade number(first request) returns?

    It should be able handle the next request but if you are expecting a large volume of requests, you will quickly run out of resources.

    I am not familliar with concurrency, and I thought I should not worry about these in Azure function , for example, I just make a sync http request in my Azure function and it will be managed by Azure or someone else? But it seems not, so I have to know is it right to call http request in Azure function.

    An Azure Functions in the end is still custom code and this code is responsible to be as efficient as possible to fully utilize resources.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.