Bringing back .NET 7 sample temporarily

This commit is contained in:
Matthew Henderson 2024-11-13 17:40:30 -08:00 коммит произвёл Fabio Cavalcante
Родитель e601cb3aaa
Коммит 28829c4cd5
2 изменённых файлов: 87 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,65 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace Net7Worker
{
public class EventHubCancellationToken
{
private readonly ILogger _logger;
public EventHubCancellationToken(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<EventHubCancellationToken>();
}
// Sample showing how to handle a cancellation token being received
// In this example, the function invocation status will be "Cancelled"
//<docsnippet_cancellation_token_throw>
[Function(nameof(ThrowOnCancellation))]
public async Task ThrowOnCancellation(
[EventHubTrigger("sample-workitem-1", Connection = "EventHubConnection")] string[] messages,
FunctionContext context,
CancellationToken cancellationToken)
{
_logger.LogInformation("C# EventHub {functionName} trigger function processing a request.", nameof(ThrowOnCancellation));
foreach (var message in messages)
{
cancellationToken.ThrowIfCancellationRequested();
await Task.Delay(6000); // task delay to simulate message processing
_logger.LogInformation("Message '{msg}' was processed.", message);
}
}
//</docsnippet_cancellation_token_throw>
// Sample showing how to take precautionary/clean up actions if a cancellation token is received
// In this example, the function invocation status will be "Successful"
//<docsnippet_cancellation_token_cleanup>
[Function(nameof(HandleCancellationCleanup))]
public async Task HandleCancellationCleanup(
[EventHubTrigger("sample-workitem-2", Connection = "EventHubConnection")] string[] messages,
FunctionContext context,
CancellationToken cancellationToken)
{
_logger.LogInformation("C# EventHub {functionName} trigger function processing a request.", nameof(HandleCancellationCleanup));
foreach (var message in messages)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogInformation("A cancellation token was received, taking precautionary actions.");
// Take precautions like noting how far along you are with processing the batch
_logger.LogInformation("Precautionary activities complete.");
break;
}
await Task.Delay(6000); // task delay to simulate message processing
_logger.LogInformation("Message '{msg}' was processed.", message);
}
}
//</docsnippet_cancellation_token_cleanup>
}
}

Просмотреть файл

@ -0,0 +1,22 @@
# Former sample for .NET 7 Worker
This sample has been removed, as support for .NET 7 ended on May 14, 2024. The `EventHubCancellationToken.cs` file is being temporarily preserved.
### EventHubCancellationToken.cs
Demonstrates how to use the Cancellation Token parameter binding in the context of an
EventHub trigger sample where we are processing multiple messages.
- `ThrowOnCancellation`
- shows how to throw when a cancellation token is received
- the status of the function will be "Cancelled"
- `HandleCancellationCleanup`
- shows how to take precautionary/clean up actions if a cancellation token is received
- the status of the function will be "Successful"
Cancellation tokens are signalled by the platform, the use cases supported today are:
1. Sudden host shutdown or host restart
2. Function timeout (where function execution has exceeded the timeout limit)
1. You can try this out by setting function timeout to 5 seconds in
the host.json file: `"functionTimeout": "00:00:05"`