Cosmos DB 的 Azure Functions 更改提要提要不会 运行:找不到工作职能

Azure Function for Cosmos DB change feed feed won't run: No job functions found

我正在编写一个 Azure Function 应用程序,它将订阅 Cosmos DB 更改源。当我在本地 (F5) 使用 Visual Studio 2019 到 运行 应用程序时,我在 CLI 上收到以下错误:

Azure Function Core Tools reports "No job functions found."

完整的代码片段如下:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;

namespace ZZZ.ChangeFeedSubscriber
{
    public static class ChangeFeedSubscriber
    {
        [FunctionName("ChangeFeedSubscriber")]
        public static void Run([CosmosDBTrigger(
            databaseName: "XXX",
            collectionName: "YYY",
            ConnectionStringSetting = "XXX",
            LeaseCollectionName = "leases")] IReadOnlyList<Doc> docs, FunctionContext context)
        {
            var logger = context.GetLogger("ChangeFeedSubscriber");

            if (docs != null && docs.Count > 0)
            {
                logger.LogInformation("Documents modified: " + docs.Count);

                foreach (var doc in docs)
                {
                    logger.LogInformation("ID: " + doc.id);
                }
            }
        }
    }
}

我试图在应用程序参数上设置“--verbose”以查看日志输出,但它引发了错误。

Adding "--verbose" throws an error.

Result of adding "--verbose" to application arguments.

我也尝试设置“start --verbose”,但它也引发了错误。

Adding "start --verbose" also throws an error.

Result of adding "start --verbose" to application arguments.

我不知道此时我还能检查什么。应用程序无法启动,根据我所做的搜索,我看不到日志输出。

如有任何帮助,我们将不胜感激。 TIA!

看来您在此处混合了进程内模型和 out-of-process 模型,这导致了问题。

根据代码,我假设您有一个进程外(独立工作程序)函数应用程序。但是你的第二行是一个 using 语句来导入 Microsoft.Azure.WebJobs 命名空间。我还看到您正在使用来自 Microsoft.Azure.WebJobs 包的 FunctionName 属性。

对于进程外函数应用程序,您不应使用 webjobs 包。相反,您应该使用 Microsoft.Azure.Functions.Worker.Extensions

中的等效包

要修复,请打开您的 .csproj 文件并删除 Microsoft.Azure.WebJobs.Extensions.CosmosDB 包。为 Microsoft.Azure.Functions.Worker.Extensions.CosmosDB 添加一个新条目(进程外工作版本)。您也可以使用 nuget 包管理器 UI 来做同样的事情。

更改后,您的 csproj 文件将如下所示

<ItemGroup>
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="3.0.9" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.3" OutputItemType="Analyzer" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.1.0" />
</ItemGroup>

还要确保您现在使用的是 Function 属性而不是 FunctionName 并删除了用于导入 Microsoft.Azure.WebJobs 命名空间的 using 语句。

[Function("Function1")]                
public static void Run([CosmosDBTrigger(

进行此更改后,您的功能将会被发现。