首页 > 解决方案 > 无法在本地运行 Cosmos DB 更改源触发 Azure 函数

问题描述

我无法在本地运行 Cosmos DB Change Feed Trigger 功能。

Cosmos DB 更改源触发 Azure 函数:

public static class NotificationChangeFeed
    {
        [FunctionName("NotificationChangeFeed")]
        public static async Task Run([CosmosDBTrigger(
            databaseName: "FleetHubNotifications",
            collectionName: "Notification",
            ConnectionStringSetting = "CosmosDBConnection",
            CreateLeaseCollectionIfNotExists = true,
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input,
            [Inject] ILoggingService loggingService,
            [Inject] IEmailProcessor emailProcessor)
        {
            var logger = new Logger(loggingService);

            try
            {
                if (input != null && input.Count > 0)
                {
                    foreach (Document document in input)
                    {
                        string requestBody = document.ToString();
                        var notification = requestBody.AsPoco<Notification>();

                        var result = await emailProcessor.HandleEmailAsync(notification, logger);

                        if (result)
                        {
                            logger.Info($"Email Notification sent successfully for file name: {document.Id}");
                        }
                        else
                        {
                            logger.Warning($"Unable to process document for Email Notification for file with name: {document.Id}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Unable to process Documents for Email Notification for Files: {input?.Count}", ex,
                    nameof(NotificationChangeFeed));
                throw;
            }
        }
    }

local.settings.json

{
  "IsEncrypted": "false",
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard ": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "CosmosDbId": "FleetHubNotifications",
    //Localhost
    "CosmoDbAuthKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
    "CosmoDbEndpoint": "https://localhost:8081/",
    "CosmosDBConnection": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
}
}

当我按 F5 时,它卡在控制台窗口中。(如下面的屏幕截图所示)

也不能调用 http 触发函数。调用时出现以下错误:

错误:连接 ECONNREFUSED 127.0.0.1:7071

有什么想法吗?

在此处输入图像描述

标签: c#azureazure-cosmosdbazure-cosmosdb-changefeed

解决方案


是的,在那部分你只能得到 http 函数的端点。其他函数也被初始化并等待任何类型的事件。哟可以在这里看到:找到以下函数,4个http触发其他2个是blob触发。

在此处输入图像描述

如果要调试 NotificationChangeFeed 函数,则必须在数据库上创建一个新文档并让该函数运行并等待该事件。您将在控制台中看到遥测数据,您可以调试该功能。


推荐阅读