You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-dotnet-class-library.md
+36-12Lines changed: 36 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -295,29 +295,53 @@ You can't use `out` parameters in async functions. For output bindings, use the
295
295
296
296
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.
297
297
298
-
The following example shows how to check for impending function termination.
298
+
This is especially the case when you have functions that process messages in batch. Consider the following version 4 (~4) Service Bus triggered function. The default template for a Service Bus triggered Azure Function is rendered to accept a single message using this parameter: ```string myQueueItem```. By changing that single message parameter to an array like: ```Message[] messages```, results in the function receiving a batch of, or multiple, messages per function invocation.
logger.WriteLine("Normal processing for queue message={0}", inputText);
317
334
}
318
335
}
319
336
}
320
337
```
338
+
A common pattern for iterating through an array uses the foreach statement. Within the foreach statement, prior to processing the message, check if a cancellation token has been generated. The most common scenario will be that the IsCancellationRequested property is false and, in that case, your coded business logic will execute. In a seldom, but realistic scenario that IsCancellationRequested is true, you will need to take some precautionary actions. For example, writing a log which stores the fact that this happened and perhaps store the portion of the message batch which has not yet been processed. If you do the latter, then your startup code needs to check if there are any message batches which experienced this shutdown procedure. The actual requirement is based on your specific scenario.
339
+
Consider that processing messages in batches is possible with an Event Hub triggered function. Notice that the events parameter is declared as an array like the following, ```EventData[] events```.
The pattern to process a batch of events is the same as the one shown for processing a batch of Service Bus messages. In each case you should check for a cancellation token prior to the processing of each item in the array, and if present, manage it as per your business requirements.
0 commit comments