Cosmos DB-更改提要触发器:“ my-function-name”的侦听器无法启动

时间:2020-07-15 08:29:35

标签: azure azure-cosmosdb azure-cosmosdb-changefeed

运行Azure Function App应用程序时出现以下错误:

[7/15/2020 8:26:08 AM]函数的侦听器 “ NotificationChangeFeed”无法启动。 [7/15/2020 8:26:08 AM] 函数“ NotificationChangeFeed”的侦听器无法执行 开始。 Microsoft.Azure.DocumentDB.Core:对象引用未设置为 对象的实例。

错误屏幕截图: enter image description here

这是我的变更Feed触发器Azure功能:

public static class NotificationChangeFeed
    {
        [FunctionName("NotificationChangeFeed")]
        public static async Task Run([CosmosDBTrigger(
            databaseName: "FleetHubNotifications",
            collectionName: "Notification",
            ConnectionStringSetting = "CosmosDBConnection",
            LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]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));
            }
        }
    }

local.settings.json:

{
  "IsEncrypted": "false",
  "Values": {
    "AzureWebJobsStorage": "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==",
}
}

1 个答案:

答案 0 :(得分:0)

本地存储模拟器的连接字符串正确。

如果您的防火墙限制func访问存储帐户,则可能会报告此错误。防火墙是侦听器无法访问虚拟存储模拟器的原因之一。

在本地运行该功能时,除httptrigger之外的所有触发器都需要使用Storage Emulator。如果防火墙限制了侦听器对虚拟存储的访问,则执行功能时可能会出现问题。

尝试禁用防火墙,看看是否可以解决问题。

当然,还有可能未打开Storage Emulator服务。尝试键入

"%programfiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" status

在cmd中检查状态。

如果返回false,请输入以下命令以启动Storage Emulator:

"%programfiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init
"%programfiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start

总结:

此监听器无法启动通常有以下三个原因。

1。连接字符串错误阻止连接,

2。防火墙已设置

3。某些服务未打开。