0

运行我的 azure function app 应用程序时出现以下错误:

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

错误截图: 在此处输入图像描述

这是我的更改提要触发器 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==",
}
}
4

1 回答 1

0

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

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

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

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

当然,也有可能是 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

总结一下:

这个监听器不能启动一般是有3个原因。

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

2.防火墙设置

3.部分服务未开启。

于 2020-07-15T09:04:51.560 回答