The Azure Functions .NET worker has been out for a while, and it finally enabled developers to use C# 9 and .NET 5 on function apps. This is a new way to create C# function apps, it now has a
Program.cs class, with the good old generic Host builder and all the extension methods that we're used to see on regular ASP.NET Core and Worker service projects. This is how it can look:
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; var host = new HostBuilder() .ConfigureAppConfiguration(builder => builder.AddJsonFile("appsettings.json")) .ConfigureFunctionsWorkerDefaults() .ConfigureServices(Startup.ConfigureServices) .Build(); await host.RunAsync();
Note that this is using top-level statements. This came with C# 9 and it basically hides the Program class and its namespace, removing some boilerplate and making it easier to understand for newcomers, looks nice!
If you want to run the .NET 5 function apps on Docker containers, as of the day of writing this, the Dockerfile generated by
func init --docker --worker-runtime dotnetIsolated does not build correctly, you'll encounter the following error:
The framework 'Microsoft.NETCore.App', version '3.1.0' was not found.
This is because you'll still need .NET Core 3.1 installed for it to work. I don't know why this wasn't fixed in the core tools template, but until it's fixed, you can use the following workaround: copying the 3.1 SDK into the image.
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS installer-env # Add the line below and it'll build successfully COPY --from=mcr.microsoft.com/dotnet/sdk:3.1 /usr/share/dotnet /usr/share/dotnet COPY . /src/dotnet-function-app RUN cd /src/dotnet-function-app && \ mkdir -p /home/site/wwwroot && \ dotnet publish src/functions/*.csproj --output /home/site/wwwroot # To enable ssh & remote debugging on app service change the base image to the one below # FROM mcr.microsoft.com/azure-functions/dotnet-isolated:3.0-dotnet-isolated5.0-appservice FROM mcr.microsoft.com/azure-functions/dotnet-isolated:3.0-dotnet-isolated5.0 ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]
This will hopefully get fixed soon, it's very weird to see a build error on a template like this.
If you're going to use Docker containers to run your function apps, you'll get 401 responses when testing your HTTP functions, there's a nice blog post by Martin Björkström explaining how to solve this problem, check it out!