-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bringing back .NET 7 sample temporarily
- Loading branch information
1 parent
e601cb3
commit 28829c4
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` |