'Azure function .net 6 return error 500 after deploy
I've migrated a project from .net core 3.1 to .net 6.0. I've resolved/updated all nugets and my functions runs well on local environments (I've tested on two pcs). The project has an http trigger and connection to database. When I call the api using postman and it's not necesary in logic connect to database my function return 200 on http status code and expected response body. However, when the function needs connect to database I see on log that the functions get results from database althoug all logic runs well when the function return the response return an error 500.
Any logs regarding to an exception is showed on azure functions logs, I search the data application logs files on kudu as well, but I ddidn't find anything related to the issue
Azure Logs (data in red was retrieved from database)
Project configuration
Nugets
Project
Azure Function properties
FUNCTIONS_EXTENSION_VERSION: ~4
Runtime version: ~4
FUNCTIONS_WORKER_RUNTIME: dotnet
Azure Function code
[FunctionName("FnDealSlip")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/fintech/dealslip")] HttpRequest req,
ILogger log)
{
string offerDate = req.Query["offerDate"];
try
{
return await Invoke(req, log, (request, logger) =>
{
return _dealSlipService.GetDealSlipByOfferDate(offerDate);
},
(response, httpRequest, logger) =>
{
log.LogInformation("Response: " + JsonFormartter.Serialize(response));
log.LogInformation("Response Status Code: " + JsonFormartter.Serialize(response.StatusCode));
var actionResponse = new ObjectResult(response)
{
StatusCode = response.StatusCode
};
log.LogInformation("Response actionResponseSet: true ");
return actionResponse;
});
}
catch (Exception ex)
{
log.LogError("error fn: " + ex.Message);
throw;
}
}
Solution 1:[1]
I found the error. After migrate .Net core 3.1 to .Net 6 some consideration arise:
- It's recomended that Azure functions doesn't share an azure account
- If diferents azure functions share a same storage account its name must be unique
The error was, the identifier used to link azure function to storage accounts are the first 26 charactes, so, I had differents azure functions sharing a same storing account and its names was longer thant 26 characters, for the link just took first 26 provonking a duplicity on identifiers.
Solution: Define a custom hostid for each function on azure configuration settings. e.g.
AzureFunctionsWebHost__hostId=mycustomfunctionname
more details on https://github.com/Azure/azure-functions-host/wiki/Host-IDs#host-id-collisions
Solution 2:[2]
- Because an HTTP Function is expected to return an HTTP result, consider changing it to:
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
ILogger log)
{
// your code goes here
return req.CreateResponse(HttpStatusCode.OK, "Done");
- The 500 internal errors occur even before our code is executed. When the client that uses the HTTP function returns a 500 error, there are no exceptions in the actual HTTP Trigger function.
- It's also worth noting that when the 500 error occurs, No Single function works. As a result, All HTTP Functions begin returning the 500 status code at the same moment. And none of them appear to be logging anything.
- You've checked that your code works locally, but when you publish it to an Azure function, you get a 500 error. In that instance, it appears that there is some configuration in your local.setting.json that was not created in the azure function under the application setting.
- I'd also recommend looking over Azure Functions diagnostics to figure out what's causing the 500 problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Felix David Hernandez Aldana |
Solution 2 | SuryasriKamini-MT |