From .NET 6 to .NET 8,my migration experience:Fixing missing x-forwarded headers in .NET 8 isolated Azure Functions
Introduction
We recently started migrating our applications from .NET 6 to .NET 8. The migration was quite simple on ASP.NET Core APIs, while on Azure functions on .NET 8 isolated it was sometimes difficult. In this post I will explain a problem that gave me a hard time for several days.
One of our functions, in order to work, uses a header, the X-Forwarded-Host header. This header is null when we try to retrieve its value in an HttpTrigger. The problem is that since we were setting a default value and we were not logging its value when it was null. As a result, we had silent errors that we had trouble detecting (I’ll spare you the details). Little by little, we discovered that all the “X-Forwarded” type headers are not correctly managed by .NET 8 isolated, they were null and we had to find a workaround.
The issue in image
Here the code of my HttpTrigger:
Here is the result that it produces via Postman:
As you can see the header’s value is null. Grrrrr 🙁
The solution
You guessed it, the solution is to go and search the headers at the very beginning of the Http Request and to go and retrieve the headers and restore them, because in fact it is a question of restoring headers whose value is lost…. Why? I have no idea, I simply found an issue on Github based on an issue not resolved by Microsoft: https://github.com/Azure/azure-functions-host/issues/10253.
We will implement a middleware, which, through the ASP.NET Core Integration, will allow us to read the headers at the beginning of requests and restore them in the Http Request as follows:
Note that I implemented a list, which allows to restore the wanted Headers. Then add it in the pipeline as follows:
Let’s test again out our HttpTrigger and enjoy the beauty of the fix: