我正在创建一个 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
提供正确的值并保存它。
有关更多信息,请参阅此 SO THREAD 对于类似的问题。
关于c# - Azure App Functions 应用程序 'appname' 已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72734417/