The upgrade of Azure Functions from .NET Core 3.1 to .NET 5 is significant. There's an excellent guide for the general steps required to perform the upgrade. However there's a number of (unrelated) items which are not covered by that post:
- Query params
- Dependency Injection
This post will show how to tackle these.
As part of the move to .NET 5 functions, we say goodbye to
HttpRequest and hello to
HttpRequest had a useful
Query property which allowed for the simple extraction of query parameters like so.
HttpRequestData has no such property. However, it's straightforward to make our own. It's simply a matter of using
req.Url.Query and using that:
Dependency Injection is a much more familiar shape in .NET 5 if you're familiar with .NET Core web apps. Once again we have a
Program.cs file. To get the configuration built in such a way to support both local development and when deployed to Azure, there's a few things to do. When deployed to Azure you'll likely want to read from Azure Application Settings:
To tackle both of these, you'll want to use
ConfigureAppConfiguration. A final
Program.cs might look something like this:
With this approach in place, when the application runs, it should construct a configuration driven by all the providers required to run our application.
When it comes to deploying to Azure via Bicep, there's some small tweaks required:
Applied to the resource itself the diff looks like this:
Before signing off, there's one more thing to slip in. When attempting to build .NET 5 Azure Functions with the .NET SDK alone, you'll encounter this error:
Docs on this seem to be pretty short. The closest I came to docs was this comment on Stack Overflow:
To build .NET 5 functions, the .NET Core 3 SDK is required. So this must be installed alongside the 5.0.x sdk.
So with Azure Pipelines you might have have something that looks like this:
Have fun building .NET 5 functions!