是否弃用了使用 CosmosDBTrigger 时的文档 class?

Have the Document class when using CosmosDBTrigger been deprecated?

在开发独立的 Azure 函数时是否弃用了 Microsoft.Azure.Documents 命名空间中的 Document class?

我最近 migrated/upgraded .NET6 Azure Function (v4) 从 运行 进程内到进程外 a.k.a 隔离(dotnet 隔离)。

我的 Azure 函数正在使用 CosmosDBTrigger 来处理 changefeed 事件。

迁移所需的步骤之一是包引用:

核心包:

The following packages are required to run your .NET functions in an isolated process:

扩展包:

Because functions that run in a .NET isolated process use different binding types, they require a unique set of binding extension packages.

因为我的 Azure 函数使用 CosmosDBTrigger,所以我还需要这个包参考:

我已经测试过在 Visual Studio 2022 年使用 .NET6 隔离模板项目创建一个新的 Azure Functions,以将我的代码与示例代码进行比较。不需要其他包参考。

现在我无法使用 IReadOnlyList<Document>,因为需要升级以下旧代码:

[FunctionName("CosmosTrigger")]
    public static void Run([CosmosDBTrigger(
        databaseName: "ToDoItems",
        collectionName: "Items",
        ConnectionStringSetting = "CosmosDBConnection",
        LeaseCollectionName = "leases",
        CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents,
        ILogger log)
    {
        if (documents != null && documents.Count > 0)
        {
            log.LogInformation($"Documents modified: {documents.Count}");
            log.LogInformation($"First document Id: {documents[0].Id}");
        }
    }

为此:

  public class ToDoItem
  {
      public string Id { get; set; }
      public string Description { get; set; }
  }

[Function("CosmosTrigger")]
    public static void Run([CosmosDBTrigger(
        databaseName: "databaseName",
        containerName: "containerName",
        Connection = "CosmosDBConnectionSetting",
        LeaseContainerName = "leases",
        CreateLeaseContainerIfNotExists = true)]IReadOnlyList<ToDoItem> input, ILogger log)
    {
        if (input != null && input.Count > 0)
        {
            log.LogInformation("Documents modified " + input.Count);
            log.LogInformation("First document Id " + input[0].Id);
        }
    }

将传入的 changefeed 事件反序列化为自定义 POCO class 没有任何意义,例如 ToDoItemDocument class 的强大之处在于能够根据 CosmosDB 文档的底层 JSON 结构,将 changefeed 事件反序列化为不同的自定义 POCO classes。

我不确定如何处理未来对我的 CosmosDB 文档的重大更改,目前我们有一个 documentVersion 属性 包含一个整数值,以便将 changefeed 事件反序列化为正确的自定义 POCO class.

如果反序列化 JSON 不对 ToDoItem class.

的数据结构进行数学运算,上面的示例代码将导致运行时异常

首先,您似乎使用的是 4.0.0 预览版扩展,请记住这是一个 预览版 扩展,不适用于生产。

反序列化为自定义 POCO 一直是很多客户的一大要求。必须支付从 Document 到企业 class 的性能成本对许多人来说并不理想。让人们自由地从他们的工作流程中删除双重序列化确实有意义。

如果你想要一个通用的 class 来反序列化,你可以按照 V3 migration guide and a good replacement for Document is JObject. Or if you are using System.Text.Json , JsonDocument.