Skip to content

Commit bc194c6

Browse files
author
Melony Qin
committed
add cancellation token and ready-to-run for isolated
1 parent 936bf8f commit bc194c6

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how to use a .NET isolated process to run your C# functions i
44

55
ms.service: azure-functions
66
ms.topic: conceptual
7-
ms.date: 07/06/2022
7+
ms.date: 09/29/2022
88
ms.custom: template-concept
99
recommendations: false
1010
#Customer intent: As a developer, I need to know how to create functions that run in an isolated process so that I can run my function code on current (not LTS) releases of .NET.
@@ -136,6 +136,59 @@ The following is an example of a middleware implementation which reads the `Http
136136

137137
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).
138138

139+
## Cancellation tokens
140+
141+
A function can accept a [CancellationToken](/dotnet/api/system.threading.cancellationtoken) parameter, which enables the operating system to notify your code when the function is about to be terminated. You can use this notification to make sure the function doesn't terminate unexpectedly in a way that leaves data in an inconsistent state.
142+
143+
Cancellation tokens are supported in .NET isolated functions. The following example shows how to use a cancellation token in a function:
144+
145+
146+
```csharp
147+
[Function("Function1")]
148+
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, FunctionContext executionContext, CancellationToken cancellationToken)
149+
{
150+
_logger.LogInformation("HttpTriggerWithCancellation function triggered");
151+
_logger.LogInformation("C# HTTP trigger function processed a request.");
152+
153+
try
154+
{
155+
var response = req.CreateResponse(HttpStatusCode.OK);
156+
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
157+
158+
response.WriteString("Welcome to Azure Functions!");
159+
160+
await Task.Delay(5000, cancellationToken);
161+
162+
return response;
163+
}
164+
catch (OperationCanceledException) {
165+
166+
_logger.LogInformation("Function invocation cancelled");
167+
var response = req.CreateResponse(HttpStatusCode.ServiceUnavailable);
168+
response.WriteString("Invocation cancelled");
169+
170+
return response;
171+
}
172+
173+
}
174+
```
175+
176+
## ReadyToRun
177+
178+
You can compile your function app as [ReadyToRun binaries](/dotnet/core/whats-new/dotnet-core-3-0#readytorun-images). ReadyToRun is a form of ahead-of-time compilation that can improve startup performance to help reduce the impact of [cold-start](event-driven-scaling.md#cold-start) when running in a [Consumption plan](consumption-plan.md).
179+
180+
ReadyToRun is available in .NET 3.0 and .NET 6 (in-proc and isolated) and .NET 7 and requires [version 3.0 of the Azure Functions runtime](functions-versions.md).
181+
182+
To compile your project as ReadyToRun, update your project file by adding the `<PublishReadyToRun>` and `<RuntimeIdentifier>` elements. The following is the configuration for publishing to a Windows 32-bit function app.
183+
184+
```xml
185+
<PropertyGroup>
186+
<TargetFramework>net7.0</TargetFramework>
187+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
188+
<PublishReadyToRun>true</PublishReadyToRun>
189+
</PropertyGroup>
190+
```
191+
139192
## Execution context
140193

141194
.NET isolated passes a [FunctionContext] object to your function methods. This object lets you get an [ILogger] instance to write to the logs by calling the [GetLogger] method and supplying a `categoryName` string. To learn more, see [Logging](#logging).
@@ -263,9 +316,9 @@ This section describes the current state of the functional and behavioral differ
263316
| Middleware | Not supported | [Supported](#middleware) |
264317
| Logging | [ILogger] passed to the function<br/>[ILogger&lt;T&gt;] via dependency injection | [ILogger]/[ILogger&lt;T&gt;] obtained from [FunctionContext] or via [dependency injection](#dependency-injection)|
265318
| Application Insights dependencies | [Supported](functions-monitoring.md#dependencies) | [Supported (public preview)](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights) |
266-
| Cancellation tokens | [Supported](functions-dotnet-class-library.md#cancellation-tokens) | Not supported |
319+
| Cancellation tokens | [Supported](functions-dotnet-class-library.md#cancellation-tokens) | [Supported](#cancellation-token) |
267320
| Cold start times<sup>2</sup> | (Baseline) | Additionally includes process launch |
268-
| ReadyToRun | [Supported](functions-dotnet-class-library.md#readytorun) | _TBD_ |
321+
| ReadyToRun | [Supported](functions-dotnet-class-library.md#readytorun) | [Supported](#readytorun) |
269322

270323
<sup>1</sup> When you need to interact with a service using parameters determined at runtime, using the corresponding service SDKs directly is recommended over using imperative bindings. The SDKs are less verbose, cover more scenarios, and have advantages for error handling and debugging purposes. This recommendation applies to both models.
271324

articles/azure-functions/functions-dotnet-class-library.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,22 @@ If you install the Core Tools using the Windows installer (MSI) package or by us
217217

218218
You can compile your function app as [ReadyToRun binaries](/dotnet/core/whats-new/dotnet-core-3-0#readytorun-images). ReadyToRun is a form of ahead-of-time compilation that can improve startup performance to help reduce the impact of [cold-start](event-driven-scaling.md#cold-start) when running in a [Consumption plan](consumption-plan.md).
219219

220-
ReadyToRun is available in .NET 3.0 and requires [version 3.0 of the Azure Functions runtime](functions-versions.md).
220+
ReadyToRun is available in .NET 3.0 and .NET 6 (in-proc and isolated) and .NET 7 and requires [version 3.0 of the Azure Functions runtime](functions-versions.md).
221221

222222
To compile your project as ReadyToRun, update your project file by adding the `<PublishReadyToRun>` and `<RuntimeIdentifier>` elements. The following is the configuration for publishing to a Windows 32-bit function app.
223223

224224
```xml
225225
<PropertyGroup>
226-
<TargetFramework>netcoreapp3.1</TargetFramework>
227-
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
226+
<TargetFramework>net6.0</TargetFramework>
227+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
228228
<PublishReadyToRun>true</PublishReadyToRun>
229229
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
230230
</PropertyGroup>
231231
```
232232

233233
> [!IMPORTANT]
234-
> ReadyToRun currently doesn't support cross-compilation. You must build your app on the same platform as the deployment target. Also, pay attention to the "bitness" that is configured in your function app. For example, if your function app in Azure is Windows 64-bit, you must compile your app on Windows with `win-x64` as the [runtime identifier](/dotnet/core/rid-catalog).
234+
> Starting in .NET 6, support for Composite ReadyToRun compilation has been added. However, In .NET 6, Composite ReadyToRun is only supported for self-contained deployment.
235+
Composite ReadyToRun compiles a set of assemblies that must be distributed together. This has the advantage that the compiler is able to perform better optimizations and reduces the set of methods that cannot be compiled via the ReadyToRun process. However, as a tradeoff, compilation speed is significantly decreased, and the overall file size of the application is significantly increased. Check out [ReadyToRun Cross platform and architecture restrictions](/dotnet/core/deploying/ready-to-run).
235236

236237
You can also build your app with ReadyToRun from the command line. For more information, see the `-p:PublishReadyToRun=true` option in [`dotnet publish`](/dotnet/core/tools/dotnet-publish).
237238

0 commit comments

Comments
 (0)