c# - Azure App Functions 应用程序 'appname' 已关闭

标签 c# azure azure-functions azure-eventhub

我正在创建一个 Azure 应用程序函数,用于从事件中心读取数据并写入另一个事件中心。最初,我只是从事件中心读取并记录一些消息,并且工作正常。

原始代码

        [FunctionName("EHFunction")]
        public static async Task Run(
            [EventHubTrigger("ehquery", Connection = "EventHubConnectionString")] EventData[] events,
            ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    // Read event hub event which contains link to a blob with messages. Read messages from the blob storage endpoint.

                    foreach (var blobEntry in blobData)
                    {
                        foreach (var record in blobEntry.Records)
                        {
                            var recordStr = record.ToString();
                            log.LogInformation($"Record: {recordStr}");
                        }
                    }
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    log.LogCritical(e.Message);
                    exceptions.Add(e);
                }
            }

这效果很好。现在我更新了代码以将记录写入新的事件中心:

        [FunctionName("EHFunction")]
        public static async Task Run(
            [EventHubTrigger("ehquery", Connection = "EventHubConnectionString")] EventData[] events,
            [EventHub("ehquery-output", Connection = "EventHubOutputConnectionAppSetting")] IAsyncCollector<string> outputEvents,
            ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    // Read event hub event which contains link to a blob with messages. Read messages from the blob storage endpoint.

                    foreach (var blobEntry in blobData)
                    {
                        foreach (var record in blobEntry.Records)
                        {
                            var recordStr = record.ToString();
                            await outputEvents.AddAsync(recordStr);
                        }
                    }
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    log.LogCritical(e.Message);
                    exceptions.Add(e);
                }
            }

当我发布此函数时,我在 eventlog.xml 文件中看到以下消息

<EventData>
<Data>Application 'C:\Program Files (x86)\SiteExtensions\Functions\3.7.1\64bit\' started successfully.</Data>
<Data>Process Id: 3776.</Data>
<Data>File Version: 13.1.22054.23. Description: IIS ASP.NET Core Module V2 Request Handler. Commit: 509f6badec2f3162f0e50330cd9107e5624b379b</Data>
</EventData>
</Event>
<EventData>
<Data>Application 'MACHINE/WEBROOT/APPHOST/EHFunction' has shutdown.</Data>
<Data>Process Id: 4624.</Data>
<Data>File Version: 13.1.22054.23. Description: IIS ASP.NET Core Module V2 Request Handler. Commit: 509f6badec2f3162f0e50330cd9107e5624b379b</Data>
</EventData>
</Event>

这是我的 local.settings.json 供引用

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "EventHubConnectionString": "Endpoint=sb://{inputeventhubconnectionstring}",
    "EventHubOutputConnectionAppSetting": "Endpoint=sb://{outputeventhubconnectionstring}",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

为什么该函数无法写入事件中心?我做错了什么?

最佳答案

感谢@Gengis Khan,您在评论中提到了对您有用的解决方案。我正在重新发布相同的解决方案,以便其他 SO 社区成员可以从中受益。

Why is the function unable to write to event hub? What am I doing wrong?

为了解决上述问题,当使用 EventHub 部署 Azure 函数时,如 local.settings.json 中所示,我们为 eventhubOutputConnectionAppSetting 提供值。所以将函数应用程序部署到 azure 时,无法读取 local.settings.json 文件,我们需要在门户上的函数中添加新的应用程序设置(通过配置 -> 新应用程序设置) 本身,为 eventhubOutputConnectionAppSetting 提供正确的值并保存它。

enter image description here

有关更多信息,请参阅此 SO THREAD 对于类似的问题

关于c# - Azure App Functions 应用程序 'appname' 已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72734417/

相关文章:

c# - 可以连接到命名管道的客户端数

c# - 理解委托(delegate)逆变的有用性

azure - 在 Azure WebJobs 中使用 NLog 进行日志记录

azure - 公共(public)域下的数百个 Azure Functions

c# - 在 C# 的对象列表中打印出 1 个对象

c# - 序列化时使用 Json.NET 的特定属性名称

Azure 应用程序状态页面

sql-server - 将 Azure SQL 数据库从 P15 (2TB) 降级到 P4 (500GB)

azure-functions - Azure Function - 防止多次调用

c# - 无法加载文件或程序集“Microsoft.Extensions.Logging,版本=7.0.0.0,文化=中性,系统找不到指定的文件”