question

MerryChristmas avatar image
MerryChristmas asked

Does Azure function can be sync

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.

Here is my questions:

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.



10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

-- My game backend send an ExecuteCloudScript request to Playfab, which calls an Azure function deployed.

You should use ExecuteFunction to call Azure Functions.

-- It seems that this Azure function will be low efficient?

Asynchronous code is not necessarily more efficient than synchronous code.

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

If there are multiple requests, Azure functions will add function instances to respond. Your code will not affect the operation of Azure Functions.

-- 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 I don't know the implementation of client.Execute(request), if this is used in the official Alipay documentation, it will be no problem in Azure function.
  1. string steamid = req.Query["steamid"];
  2. string amount = req.Query["amount"];
  3. string subject = req.Query["subject"];

This part of the code cannot get the value you pass through Functionparamater of the ExecuteFunction. You should follow the document to deserialize the req.body to get the value.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

MerryChristmas avatar image MerryChristmas commented ·

Thank you so much, by the way the client.Execute(request) is used in the official Alipay documentation.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.