Home > Software engineering >  Azure Function HttpTrigger returns 500, if function runs longer then 5 seconds. Why?
Azure Function HttpTrigger returns 500, if function runs longer then 5 seconds. Why?

Time:11-01

I run a HttpTrigger Azure Function which runs above 5 seconds. Locally it works like a charm but deployed it returns "(500) Internal Server Error".

Steps to reproduce (fails):

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

namespace MyTest.TestFunction
{
    public class TestFunction
    {
        [FunctionName("TestFunction")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
           
            log.LogInformation("Before sleep");
            Thread.Sleep(7000);
            log.LogInformation("After sleep");

            return new OkObjectResult($"Hello");
        }
    }
}

Steps to reproduce (works):

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

namespace MyTest.TestFunction
{
    public class TestFunction
    {
        [FunctionName("TestFunction")]
        public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
           
            log.LogInformation("Before sleep");
            Thread.Sleep(2000);
            log.LogInformation("After sleep");

            return new OkObjectResult($"Hello");
        }
    }
}

My host.json looks like this:

{
  "version": "2.0",
  "logging": {
  "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "functionTimeout": "00:15:00",
}

FYI: The non-test function does not use sleep but just takes longer then 5 seconds.

CodePudding user response:

The 500 error is not helpful to solve this problem, you need to check the specific error of the azure function. You can use Application Insights to get more details. The function must configure the corresponding application insights before you can view the log on the portal.

Also, try to add the extension bundle property to your host.json file, like so:

{
  "version": "2.0",
  "logging": {
  "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "functionTimeout": "00:15:00",
  "extensionBundle": {
      "id": "Microsoft.Azure.Functions.ExtensionBundle",
      "version": "[2.*, 3.0.0)"
  }
}

CodePudding user response:

I've created an Azure Httptrigger Function in local and ran successfully as shown below:

enter image description here

I deployed it to the Azure portal and got the following error:

enter image description here

As per above error, I made changes in host.json file as "Microsoft.Azure.WebJobs.Script.FunctionTimeout must be greater than 00.00.01 and less than 00.10.00" :

host.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
       }
    }
},
"functionTimeout": "00:10:00"
}
 

After making changes in host.json only, I deployed it to Azure again and received 200 OK response by setting:

Thread.Sleep(7000);

enter image description here

Thread.Sleep(2000);

enter image description here

  • Related