如何在 Azure 函数 eventhub 触发器中处理 CancellationToken

How to handle CancellationToken in Azure function eventhub trigger

在 Azure 函数事件中心触发器 (v3) 中,它在 Run 方法中接收取消标记。当发出取消信号时,这意味着它正在关闭服务器。例如,如果我使用 httpClient 将此令牌发送到 Get 操作,它将抛出 TaskCanceledException 并且函数将结束。

此函数正在处理的事件是否会发送到另一台服务器上该函数的另一个实例,或者它们会丢失吗?取消应该以不同的方式处理吗?

[FunctionName(nameof(MyFunction)), FixedDelayRetry(10, "00:00:15")]
public async Task RunAsync(
   [EventHubTrigger("%InEventHubName%", 
     Connection = "InEventHubConnectionString", 
     ConsumerGroup = "%ConsumerGroup%")] 
   EventData[] events,
   PartitionContext partitionContext,
   CancellationToken cancellationToken)
{
       foreach (var ev in events)
       {
            var response = await _httpClient.GetAsync("http://example.com/fetch?key=" + ev.Properties["Key"],
                             cancellationToken);
            await Process(response, cancellationToken);
       }
}

Will the events that this function was processing be sent to another instance of the function on another server or are they lost?

They are lost:

Unhandled exceptions may cause you to lose messages. Executions that result in an exception will continue to progress the pointer.


Should cancellation be handle in a different way?

您可以选择忽略取消。这可能最适合这种情况。