{
"$type": "site.standard.document",
"canonicalUrl": "https://johnnyreilly.com/posts/azure-functions-dotnet-5-query-params-di-bicep",
"description": "The upgrade of Azure Functions from .NET Core 3.1 to .NET 5 is significant. This post shows part of the upgrade: Query params, Dependency Injection, Bicep & Build",
"path": "/posts/azure-functions-dotnet-5-query-params-di-bicep",
"publishedAt": "2021-06-11T00:00:00.000Z",
"site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
"tags": [
"azure functions",
"bicep",
"asp.net"
],
"textContent": "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:\n\n\n\n- Query params\n- Dependency Injection\n- Bicep\n- Build\n\nThis post will show how to tackle these.\n\nQuery params\n\nAs part of the move to .NET 5 functions, we say goodbye to HttpRequest and hello to HttpRequestData. Now HttpRequest had a useful Query property which allowed for the simple extraction of query parameters like so.\n\nHttpRequestData has no such property. However, it's straightforward to make our own. It's simply a matter of using System.Web.HttpUtility.ParseQueryString on req.Url.Query and using that:\n\nDependency Injection, local development and Azure Application Settings\n\nDependency 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:\n\nTo tackle both of these, you'll want to use AddJsonFile and AddEnvironmentVariables in ConfigureAppConfiguration. A final Program.cs might look something like this:\n\nWith this approach in place, when the application runs, it should construct a configuration driven by all the providers required to run our application.\n\nBicep\n\nWhen it comes to deploying to Azure via Bicep, there's some small tweaks required:\n\n- appSettings.FUNCTIONS_WORKER_RUNTIME becomes dotnet-isolated\n- linuxFxVersion becomes DOTNET-ISOLATED|5.0\n\nApplied to the resource itself the diff looks like this:\n\nBuilding .NET 5 functions\n\nBefore 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:\n\nDocs on this seem to be pretty short. The closest I came to docs was this comment on Stack Overflow:\n\n> To build .NET 5 functions, the .NET Core 3 SDK is required. So this must be installed alongside the 5.0.x sdk.\n\nSo with Azure Pipelines you might have have something that looks like this:\n\nHave fun building .NET 5 functions!",
"title": "Azure Functions and .NET 5: Query params, Dependency Injection, Bicep & Build"
}