Search code examples
c#azureazure-sql-databaseazure-functionsinternal-server-error

Why I get Internal Server Error after publishing Azure Function?


I've made an API in Azure Functions that uses an Azure Sql database. I have this route:

    [FunctionName("GetAllClassesForTeacher")]
    public IActionResult GetAllClassesForTeacher(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req,
     ILogger log)
    {
        var headers = req.Headers;
        if (headers.TryGetValues("TeacherId", out var Teacher))
        {
            int TeacherId = Convert.ToInt32(Teacher.First());

            var result = _classDistributionServices.GetAllClassesForTeacher(TeacherId);

            if (result != null)
            {
                return new OkObjectResult(result);
            }
            return new NotFoundResult();
        }
        return new NotFoundResult();
    }

When tested localy, it works everytime. But after publishing it on Azure Portal, I get back from postman:

{
"statusCode": 500,
"message": "Internal server error",
"activityId": "f98c4112-0c31-4841-99a5-c79dffa41d86"

What I've tried/did until now:

  • to take the IP from Azure Function and register it in Firewalls and virtual networks from Azure Sql Server.
  • I've made sure that the ConnectionString from local.settings.json it is uploaded on portal.
  • I've declared this route in API Management.

But still the error persists. I have a feeling that is still because of database firewall even if I've registered the right IP?

Can you please help me?


Solution

  • There a few way for you to figure out what is the problem:

    1-Enable Application Insights and use the Live Metrics to capture the error

    2-Use Kudu and check for more details about the error (e.g. https://myfunctionapp.scm.azurewebsites.net/azurejobs/#/functions)

    3-You can also wrap your code with a try/catch and throw details of the exception in the response output.