The .NET Isolated Worker Model allows developers to create Azure Functions using a .NET console app project that targets a supported .NET runtime. Compared to the in-process model, the Isolated Worker Model allows for greater flexibility, better performance, and improved debugging experience.
In this guide, I'll take you through upgrading your existing Azure Functions project to .NET 8, utilizing the isolated worker model.
Updating the .csproj File
The first step is converting the project file and updating dependencies.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<SdkVersion>1.18.1</SdkVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.23.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="$(SdkVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions"
Version="1.3.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.6.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.4.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
</ItemGroup>
</Project>
Make the following changes:
- Set
<TargetFramework>tonet8.0 - Set
<AzureFunctionsVersion>tov4. - Add
<OutputType>Exe</OutputType>.
Updating the host.json File
Your host.json file plays a critical role in configuring the runtime behavior of your Azure Functions. Below is a sample configuration for an upgraded .NET 8 isolated function:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
Updating the Program.cs File
The Program.cs file replaces FunctionStartup and serves as the application’s entry point.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace FunctionApp
{
class Program
{
static async Task Main(string[] args)
{
// #if DEBUG
// Debugger.Launch();
// #endif
//<docsnippet_startup>
var host = new HostBuilder()
//<docsnippet_configure_defaults>
.ConfigureFunctionsWorkerDefaults()
//</docsnippet_configure_defaults>
//<docsnippet_dependency_injection>
.ConfigureServices(s =>
{
s.AddApplicationInsightsTelemetryWorkerService();
s.ConfigureFunctionsApplicationInsights();
s.Configure<LoggerFilterOptions>(options =>
{
// The Application Insights SDK adds a default logging filter that instructs ILogger to capture only Warning and more severe logs. Application Insights requires an explicit override.
// Log levels can also be configured using appsettings.json. For more information, see https://learn.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs
LoggerFilterRule? toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
== "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");
if (toRemove is not null)
{
options.Rules.Remove(toRemove);
}
});
})
//</docsnippet_dependency_injection>
.Build();
//</docsnippet_startup>
//<docsnippet_host_run>
await host.RunAsync();
//</docsnippet_host_run>
}
}
}
Updating local.settings.json
Modify FUNCTIONS_WORKER_RUNTIME to dotnet-isolated.
After you have done this, open up the terminal and run the following commands to build and debug the file.
- dotnet clean
- dotnet restore
- dotnet build
- Try running the function using the command func start. If you are getting an error stating that it found 2 csproj file instead of 1 – that is the worker dependencies creating a csproj file, remove them by using the following command:
Remove-Item -Recurse -Force objThen run func start again, now it would run without any issues. - Even though the DLL is running, the debugger is not attached, so your breakpoints won't be hit. In order to do that, after we have done the func start, we should attach the debugger.
- Click the green play button, which would create a prompt and ask to select the process for debugging, select the DLL of the project, and click it. Now the debugging is enabled, and your breakpoints will be hit.
By upgrading to the .NET 8 Isolated Worker Model, you improve the performance and maintainability of your Azure Functions. By going step-by-step through this guide, you can ensure that you follow the correct process while also taking advantage of the improvements in .NET and Azure Functions.
