Home > Net >  Azure Function Net5 Async Probleme
Azure Function Net5 Async Probleme

Time:11-07

I have a problem with azure function only when I publish.

the code run not in order, all the functions run at the same time.

ex :

var logger = context.GetLogger(nameof(MibProposalRequest));
        logger.Log(LogLevel.Warning, $"Début de la fonction : MibProposalRequest {DateTime.UtcNow}");
        
        try
        {
            var getStringResponse = await this.GetStringAsync(req, logger).ConfigureAwait(false);
            if (getStringResponse.Error != null)
            {
                return new HttpResponseMessage()
                {
                    StatusCode = getStringResponse.Error.StatusCode,
                    ReasonPhrase = getStringResponse.Error.Message
                };
            }
            else
            {
                var requestBody = getStringResponse.Response.ToString();
                var deserializeRequestBodyResponse = await this.DeserializeRequestBodyAsync(requestBody, logger).ConfigureAwait(false);
                if (deserializeRequestBodyResponse.Error != null)
                {
                    return new HttpResponseMessage()
                    {
                        StatusCode = deserializeRequestBodyResponse.Error.StatusCode,
                        ReasonPhrase = deserializeRequestBodyResponse.Error.Message
                    };
                }

In this example I need to get the response of the function GETStringAsync and after go to the function : DeserializeRequestBodyAsync.

If I run the azure function in local (debug) it's all ok, everything it's in order. First log the function start. After Get the string end deserializeObject.

But when I publish the function and I test it, when I'm loocking at the log in portal.azure. All the function run not in good order. So sometimes I get an error from deserializeObject function because no string to deserialize. But its suppose to do the GetStringAsync function first.

Why this append? Image of the log

as you can see the function start log is not the first to show up why ? looks like all the code run completely not in order.

should be like this : Log of vs debug

Everything is good each functions run in good order (so no problems)

Why this append only when the function is published ??

Ps: I tried to run the function not async but nothing change. PS2: I tried to put all the code in the same function (Run) nothing changed. PS3: I tried to put task.wait nothing changed.

I need help please 2 days I'm on this.

CodePudding user response:

https://azure.microsoft.com/en-us/updates/general-availability-azure-functions-supports-net-5-in-production/?cdn=disable

From my experience this was a pain to get to work with Azure Functions on .NET 5. Either roll back to 3.1 or wait till 11/9/2021 for .NET 6 to release out of preview. .NEt 6 preview is allowed on Azure for testing.

Are you running .NET 5 in Azure in isolated mode? See link above.

"To support .NET 5, Azure Functions introduces a new isolated process model that runs .NET function apps in a separate worker process outside the Azure Functions host runtime."

CodePudding user response:

Ok I will try to be more clear in the post :

I have made a azureFunction on visualStudio.

Inside the function I have this :

[Function("MibProposalRequest")]
    public async Task<HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req, FunctionContext context)
    {
        var logger = context.GetLogger(nameof(MibProposalRequest));
        logger.Log(LogLevel.Warning, $"Début de la fonction : MibProposalRequest {DateTime.UtcNow}");
        
        try
        {
            var getStringResponse = await this.GetStringAsync(req, logger).ConfigureAwait(false);
            if (getStringResponse.Error != null)
            {
                return new HttpResponseMessage()
                {
                    StatusCode = getStringResponse.Error.StatusCode,
                    ReasonPhrase = getStringResponse.Error.Message
                };
            }
            else
            {
                var requestBody = getStringResponse.Response.ToString();
                var deserializeRequestBodyResponse = await this.DeserializeRequestBodyAsync(requestBody, logger).ConfigureAwait(false);
                if (deserializeRequestBodyResponse.Error != null)
                {
                    return new HttpResponseMessage()
                    {
                        StatusCode = deserializeRequestBodyResponse.Error.StatusCode,
                        ReasonPhrase = deserializeRequestBodyResponse.Error.Message
                    };
                }

I need to get the response of GetStringAsync before go to the DeserializeRequestBodyAsync ok?

So if I try to execute the DeserializeRequestBodyAsync before the GetStringAsync. The program will send me a null reference execption ok?!

var answer1 = function 1;

var answer2 = function2(need the answer1 Here).

this is why I need to exec the GetStringAsync. If I try to run in visual studio no problem !!! everything it's in order 1 and after 2.

But when I publish the function, the function run good but not in good order I have the function 2 run before the 1 why ?

  • Related