Skip to content

Commit 250455c

Browse files
authored
Merge pull request #270634 from mattchenderson/march-migration
Including options useful in migrations
2 parents f41de6b + f74e556 commit 250455c

7 files changed

+191
-22
lines changed

articles/azure-functions/dotnet-isolated-process-guide.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Guide for running C# Azure Functions in an isolated worker process
3-
description: Learn how to use a .NET isolated worker process to run your C# functions in Azure, which lets you run your functions on currently supported versions of .NET and .NET Framework.
3+
description: Learn how to use the .NET isolated worker model to run your C# functions in Azure, which lets you run your functions on currently supported versions of .NET and .NET Framework.
44
ms.service: azure-functions
55
ms.topic: conceptual
66
ms.date: 12/13/2023
@@ -13,7 +13,7 @@ recommendations: false
1313
#Customer intent: As a developer, I need to know how to create functions that run in an isolated worker process so that I can run my function code on current (not LTS) releases of .NET.
1414
---
1515

16-
# Guide for running C# Azure Functions in an isolated worker process
16+
# Guide for running C# Azure Functions in the isolated worker model
1717

1818
This article is an introduction to working with Azure Functions in .NET, using the isolated worker model. This model allows your project to target versions of .NET independently of other runtime components. For information about specific .NET versions supported, see [supported version](#supported-versions).
1919

@@ -111,7 +111,10 @@ The [ConfigureFunctionsWorkerDefaults] method is used to add the settings requir
111111

112112
Having access to the host builder pipeline means that you can also set any app-specific configurations during initialization. You can call the [ConfigureAppConfiguration] method on [HostBuilder] one or more times to add the configurations required by your function app. To learn more about app configuration, see [Configuration in ASP.NET Core](/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0&preserve-view=true).
113113

114-
These configurations apply to your function app running in a separate process. To make changes to the functions host or trigger and binding configuration, you still need to use the [host.json file](functions-host-json.md).
114+
These configurations apply to your function app running in a separate process. To make changes to the functions host or trigger and binding configuration, you still need to use the [host.json file](functions-host-json.md).
115+
116+
> [!NOTE]
117+
> Custom configuration sources cannot be used for configuration of triggers and bindings. Trigger and binding configuration must be available to the Functions platform, and not just your application code. You can provide this configuration through the [application settings](../app-service/configure-common.md#configure-app-settings), [Key Vault references](../app-service/app-service-key-vault-references.md?toc=%2Fazure%2Fazure-functions%2Ftoc.json), or [App Configuration references](../app-service/app-service-configuration-references.md?toc=%2Fazure%2Fazure-functions%2Ftoc.json) features.
115118
116119
### Dependency injection
117120

@@ -215,6 +218,45 @@ This is an example of a middleware implementation that reads the `HttpRequestDat
215218

216219
This middleware checks for the presence of a specific request header(x-correlationId), and when present uses the header value to stamp a response header. Otherwise, it generates a new GUID value and uses that for stamping the response header. For a more complete example of using custom middleware in your function app, see the [custom middleware reference sample](https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/CustomMiddleware).
217220

221+
### Customizing JSON serialization
222+
223+
The isolated worker model uses `System.Text.Json` by default. You can customize the behavior of the serializer by configuring services as part of your `Program.cs` file. The following example shows this using `ConfigureFunctionsWebApplication`, but it will also work for `ConfigureFunctionsWorkerDefaults`:
224+
225+
```csharp
226+
var host = new HostBuilder()
227+
.ConfigureFunctionsWebApplication((IFunctionsWorkerApplicationBuilder builder) =>
228+
{
229+
builder.Services.Configure<JsonSerializerOptions>(jsonSerializerOptions =>
230+
{
231+
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
232+
jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
233+
jsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
234+
235+
// override the default value
236+
jsonSerializerOptions.PropertyNameCaseInsensitive = false;
237+
});
238+
})
239+
.Build();
240+
```
241+
242+
You might wish to instead use JSON.NET (`Newtonsoft.Json`) for serialization. To do this, you would install the [`Microsoft.Azure.Core.NewtonsoftJson`](https://www.nuget.org/packages/Microsoft.Azure.Core.NewtonsoftJson) package. Then, in your service registration, you would reassign the `Serializer` property on the `WorkerOptions` configuration. The following example shows this using `ConfigureFunctionsWebApplication`, but it will also work for `ConfigureFunctionsWorkerDefaults`:
243+
244+
```csharp
245+
var host = new HostBuilder()
246+
.ConfigureFunctionsWebApplication((IFunctionsWorkerApplicationBuilder builder) =>
247+
{
248+
builder.Services.Configure<WorkerOptions>(workerOptions =>
249+
{
250+
var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
251+
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
252+
settings.NullValueHandling = NullValueHandling.Ignore;
253+
254+
workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
255+
});
256+
})
257+
.Build();
258+
```
259+
218260
## Methods recognized as functions
219261

220262
A function method is a public method of a public class with a `Function` attribute applied to the method and a trigger attribute applied to an input parameter, as shown in the following example:
@@ -604,7 +646,7 @@ In Visual Studio, the **Target Runtime** option in the publish profile should be
604646

605647
## Deploy to Azure Functions
606648

607-
When running in Azure, your function code project must run in either a function app or in a Linux container. The function app and other required Azure resources must exist before you deploy your code.
649+
When you deploy your function code project to Azure, it must run in either a function app or in a Linux container. The function app and other required Azure resources must exist before you deploy your code.
608650

609651
You can also deploy your function app in a Linux container. For more information, see [Working with containers and Azure Functions](functions-how-to-custom-container.md).
610652

articles/azure-functions/migrate-dotnet-to-isolated-model.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The section outlines the various changes that you need to make to your local pro
7474
7575
First, you'll convert the project file and update your dependencies. As you do, you will see build errors for the project. In subsequent steps, you'll make the corresponding changes to remove these errors.
7676

77-
### .csproj file
77+
### Project file
7878

7979
The following example is a `.csproj` project file that uses .NET 6 on version 4.x:
8080

@@ -140,7 +140,7 @@ var host = new HostBuilder()
140140
host.Run();
141141
```
142142

143-
This examples supports [ASP.NET Core integration] to use normal .NET 8 types. To use the built-in Functions HTTP types instead, replace the call to `ConfigureFunctionsWebApplication` with a call to `ConfigureFunctionsWorkerDefaults`.
143+
This example includes [ASP.NET Core integration] to improve performance and provide a familiar programming model when your app uses HTTP triggers. If you do not intend to use HTTP triggers, you can replace the call to `ConfigureFunctionsWebApplication` with a call to `ConfigureFunctionsWorkerDefaults`. If you do so, you can remove the reference to `Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore` from your project file. However, for the best performance, even for functions with other trigger types, you should keep the `FrameworkReference` to ASP.NET Core.
144144

145145
# [.NET Framework 4.8](#tab/netframework48)
146146

@@ -171,9 +171,7 @@ namespace Company.FunctionApp
171171

172172
---
173173

174-
The `Program.cs` file will replace any file that has the `FunctionsStartup` attribute, which is typically a `Startup.cs` file. In places where your `FunctionsStartup` code would reference `IFunctionsHostBuilder.Services`, you can instead add statements within the `.ConfigureServices()` method of the `HostBuilder` in your `Program.cs`. To learn more about working with `Program.cs`, see [Start-up and configuration](./dotnet-isolated-process-guide.md#start-up-and-configuration) in the isolated worker model guide.
175-
176-
Once you have moved everything from any existing `FunctionsStartup` to the `Program.cs` file, you can delete the `FunctionsStartup` attribute and the class it was applied to.
174+
[!INCLUDE [functions-dotnet-migrate-isolated-program-cs](../../includes/functions-dotnet-migrate-isolated-program-cs.md)]
177175

178176
### Function signature changes
179177

@@ -253,6 +251,16 @@ When migrating from running in-process to running in an isolated worker process,
253251

254252
The value you have configured for `AzureWebJobsStorage`` might be different. You do not need to change its value as part of the migration.
255253

254+
### host.json file
255+
256+
No changes are required to your `host.json` file. However, if your Application Insights configuration in this file from your in-process model project, you might want to make additional changes in your `Program.cs` file. The `host.json` file only controls logging from the Functions host runtime, and in the isolated worker model, some of these logs come from your application directly, giving you more control. See [Managing log levels in the isolated worker model](./dotnet-isolated-process-guide.md#managing-log-levels) for details on how to filter these logs.
257+
258+
### Other code changes
259+
260+
This section highlights other code changes to consider as you work through the migration. These changes are not needed by all applications, but you should evaluate if any are relevant to your scenarios.
261+
262+
[!INCLUDE [functions-dotnet-migrate-isolated-other-code-changes](../../includes/functions-dotnet-migrate-isolated-other-code-changes.md)]
263+
256264
### Example function migrations
257265

258266
#### HTTP trigger example

articles/azure-functions/migrate-version-1-version-4.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Choose the tab that matches your target version of .NET and the desired process
129129
> [!TIP]
130130
> If you are moving to an LTS or STS version of .NET using the isolated worker model, the [.NET Upgrade Assistant] can be used to automatically make many of the changes mentioned in the following sections.
131131
132-
### .csproj file
132+
### Project file
133133

134134
The following example is a `.csproj` project file that runs on version 1.x:
135135

@@ -216,6 +216,10 @@ var host = new HostBuilder()
216216
host.Run();
217217
```
218218

219+
This example includes [ASP.NET Core integration] to improve performance and provide a familiar programming model when your app uses HTTP triggers. If you do not intend to use HTTP triggers, you can replace the call to `ConfigureFunctionsWebApplication` with a call to `ConfigureFunctionsWorkerDefaults`. If you do so, you can remove the reference to `Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore` from your project file. However, for the best performance, even for functions with other trigger types, you should keep the `FrameworkReference` to ASP.NET Core.
220+
221+
[!INCLUDE [functions-dotnet-migrate-isolated-program-cs](../../includes/functions-dotnet-migrate-isolated-program-cs.md)]
222+
219223
# [.NET 6 (in-process)](#tab/net6-in-proc)
220224

221225
A program.cs file isn't required when running in-process.
@@ -247,6 +251,8 @@ namespace Company.FunctionApp
247251
}
248252
```
249253

254+
[!INCLUDE [functions-dotnet-migrate-isolated-program-cs](../../includes/functions-dotnet-migrate-isolated-program-cs.md)]
255+
250256
---
251257

252258
### host.json file
@@ -259,6 +265,8 @@ To run on version 4.x, you must add `"version": "2.0"` to the host.json file. Yo
259265

260266
:::code language="json" source="~/functions-quickstart-templates//Functions.Templates/ProjectTemplate_v4.x/CSharp-Isolated/host.json":::
261267

268+
The `host.json` file only controls logging from the Functions host runtime, and in the isolated worker model, some of these logs come from your application directly, giving you more control. See [Managing log levels in the isolated worker model](./dotnet-isolated-process-guide.md#managing-log-levels) for details on how to filter these logs.
269+
262270
# [.NET 6 (in-process)](#tab/net6-in-proc)
263271

264272
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/ProjectTemplate_v4.x/CSharp/host.json":::
@@ -267,6 +275,7 @@ To run on version 4.x, you must add `"version": "2.0"` to the host.json file. Yo
267275

268276
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/ProjectTemplate_v4.x/CSharp-Isolated/host.json":::
269277

278+
The `host.json` file only controls logging from the Functions host runtime, and in the isolated worker model, some of these logs come from your application directly, giving you more control. See [Managing log levels in the isolated worker model](./dotnet-isolated-process-guide.md#managing-log-levels) for details on how to filter these logs.
270279

271280
---
272281

@@ -336,6 +345,26 @@ Some key classes changed names between version 1.x and version 4.x. These change
336345

337346
There might also be class name differences in bindings. For more information, see the reference articles for the specific bindings.
338347

348+
### Other code changes
349+
350+
# [.NET 8 (isolated)](#tab/net8)
351+
352+
This section highlights other code changes to consider as you work through the migration. These changes are not needed by all applications, but you should evaluate if any are relevant to your scenarios. Make sure to check [Behavior changes after version 1.x](#behavior-changes-after-version-1x) for additional changes you might need to make to your project.
353+
354+
[!INCLUDE [functions-dotnet-migrate-isolated-other-code-changes](../../includes/functions-dotnet-migrate-isolated-other-code-changes.md)]
355+
356+
# [.NET 6 (in-process)](#tab/net6-in-proc)
357+
358+
Make sure to check [Behavior changes after version 1.x](#behavior-changes-after-version-1x) for additional changes you might need to make to your project.
359+
360+
# [.NET Framework 4.8](#tab/netframework48)
361+
362+
This section highlights other code changes to consider as you work through the migration. These changes are not needed by all applications, but you should evaluate if any are relevant to your scenarios. Make sure to check [Behavior changes after version 1.x](#behavior-changes-after-version-1x) for additional changes you might need to make to your project.
363+
364+
[!INCLUDE [functions-dotnet-migrate-isolated-other-code-changes](../../includes/functions-dotnet-migrate-isolated-other-code-changes.md)]
365+
366+
---
367+
339368
### HTTP trigger template
340369

341370
Most of the code changes between version 1.x and version 4.x can be seen in HTTP triggered functions. The HTTP trigger template for version 1.x looks like the following example:

0 commit comments

Comments
 (0)