gpt4 book ai didi

azure - 如何将更改源监视集合名称获取到我的 ChangesHander - Containers.ChangesHander (Azure Cosmos v3)

转载 作者:行者123 更新时间:2023-12-03 02:23:31 27 4
gpt4 key购买 nike

我有一项服务,可以生成多个 Changefeed 来监视多个不同的 Cosmos DB 集合。在 v1 或 2 中,ChangefeedObserver 类包含 ChangefeedObserverContext,我可以从中提取集合名称。

  public Task ProcessChangesAsync(IChangeFeedObserverContext context, IReadOnlyList<Document> deltas, CancellationToken cancellationToken)
{
string observerCollection = string.Empty;
try
{
Regex rx = new Regex(@"\b(\w+$)");
observerCollection = rx.Match(context.FeedResponse.ContentLocation).Value.ToString();

在 v3 中,您不再传递类型作为处理器,而是传递一个委托(delegate)方法,该方法的签名不再包含上下文

MS Docs Container.ChangesHandlerDelegate

Change Feed Processor

    private ChangeFeedProcessor ChangeFeedInitialize(Container leasingContainer, Container monitoringContainer, string hostName)
{
ChangeFeedProcessor changeFeedProcessor = monitoringContainer
.GetChangeFeedProcessorBuilder<Document>(hostName, this.HandleChangesAsync)
.WithInstanceName("isn")
.WithLeaseContainer(leasingContainer)
.Build();

return changeFeedProcessor;
}

private async Task HandleChangesAsync(IReadOnlyCollection<Document> changes, CancellationToken cancellationToken)
{
ILogger logger = AnalyticsHelper.BuildMeMyLogger(this.loggerFactory);

try
{
AnalyticsChangefeedProcessor changefeedProcessor = new AnalyticsChangefeedProcessor();
await changefeedProcessor.HandleChangesAsync(changes, this.analyticsContext.DataLakeStorageProvider, "CollectionName", logger);
}
catch (Exception ex)
{
logger.LogFailure($"Failed to process changes: {ex.Message}", TagIds.ExceptionAnalytics, ex);
}
}

在上面的代码中,我有一个创建 Changefeed 的基本方法(通过计时器启动),以及根据受监视的集合将处理发送到更大的类以采取操作的委托(delegate)方法。

那么,我怎样才能将这个changefeeds Monitored Collection值获取到ChangesHander中?

最佳答案

您已经有了引用,您可以注入(inject)它或引用它。

private ChangeFeedProcessor ChangeFeedInitialize(Container leasingContainer, Container monitoringContainer, string hostName)
{
ChangeFeedProcessor changeFeedProcessor = monitoringContainer
.GetChangeFeedProcessorBuilder<Document>(hostName,
(IReadOnlyCollection<Document> changes, CancellationToken cancellationToken) =>
this.HandleChangesAsync(monitoringContainer, changes, cancellationToken))
.WithInstanceName("isn")
.WithLeaseContainer(leasingContainer)
.Build();

return changeFeedProcessor;
}

private async Task HandleChangesAsync(Container monitoringContainer, IReadOnlyCollection<Document> changes, CancellationToken cancellationToken)
{
ILogger logger = AnalyticsHelper.BuildMeMyLogger(this.loggerFactory);

try
{
AnalyticsChangefeedProcessor changefeedProcessor = new AnalyticsChangefeedProcessor();
await changefeedProcessor.HandleChangesAsync(changes, this.analyticsContext.DataLakeStorageProvider, "CollectionName", logger);
}
catch (Exception ex)
{
logger.LogFailure($"Failed to process changes: {ex.Message}", TagIds.ExceptionAnalytics, ex);
}
}

关于azure - 如何将更改源监视集合名称获取到我的 ChangesHander - Containers.ChangesHander<T> (Azure Cosmos v3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67924458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com