我在长时间运行的 Azure Function App 方面遇到了问题。它基本上是读取一个管道分隔文件,根据值创建一个 Employee 对象,获取相应的员工数据库记录,然后执行数据库插入或更新。
由于花费了很长时间并且占用了 CPU 资源,因此有人建议我读取文件的每一行并将其发送到服务总线。然后在我的函数应用程序中使用另一个函数来从队列中读取并进行记录比较。
我以前从未使用过服务总线,但在 Azure 中进行了设置。现在,我尝试在 Function App 中使用 ServiceBus 输出绑定(bind)来发送消息,但我无法让它工作。我一直在关注我发现的一些不同的文章,包括以下来自 Microsoft 的文章。
我已将 ServiceBusConnection 添加到我的 local.settings.json 文件中。我从 Azure 服务总线“共享访问策略”部分提取了连接字符串。我仔细检查了设置并确保属性名称正确。但是,由于连接字符串位于返回部分,我不知道如何调试并确认它实际上拉动了连接字符串并且它是正确的。
这是我到目前为止所想到的,但我在队列中没有看到任何消息。
[FunctionName("ProcessEmployeeInput")]
[return: ServiceBus("myQueueName", Connection = "ServiceBusConnection")]
public string Run(
[BlobTrigger("%InputContainer%/%InputFolder%/{name}.txt", Connection = "StorageConnection")] Stream fileBlob, string name, ILogger log)
{
log.LogInformation($"Azure Function START.");
log.LogInformation($"Processing file {name}.");
string jsonEmployee = string.Empty;
try
{
using (var srFile = new StreamReader(fileBlob))
{
srFile.ReadLine(); //first line is a header. Just call ReadLine to skip past it.
while (!srFile.EndOfStream)
{
var record = srFile.ReadLine();
//Create an Employee object and map the properties.
Employee employee = MapEmployeeProperties(record, log);
//Serialize into text before sending to service bus
jsonEmployee = JsonConvert.SerializeObject(employee);
}
}
}
catch (Exception ex)
{
log.LogError("Error processing file. {0} | {1} | {2}", ex.Message, ex.StackTrace, ex.InnerException);
}
return jsonEmployee;
}
错误消息
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我一开始没有看到这一点,但从这条消息来看,它似乎找不到服务总线端点。由于连接位于输出绑定(bind)中,因此我可以确认它可以找到它并且它是正确的。
最佳答案
很高兴@Caverman解决了这个问题。感谢 @Jesse Squire 提供的建议有助于解决该问题。发布此内容代表您的讨论,以便对其他社区成员有益。
解决方法:-
To resolve the above issue make sure that we have following in our
host.json
file and the nuget packageAzure.Messaging.ServiceBus
is installed in the environment.
host.json
file example:-{ "version": "2.0", "extensions": { "serviceBus": { "clientRetryOptions":{ "mode": "exponential", "tryTimeout": "00:01:00", "delay": "00:00:00.80", "maxDelay": "00:01:00", "maxRetries": 3 }, "prefetchCount": 0, "transportType": "amqpWebSockets", "webProxy": "https://proxyserver:8080", "autoCompleteMessages": true, "maxAutoLockRenewalDuration": "00:05:00", "maxConcurrentCalls": 16, "maxConcurrentSessions": 8, "maxMessageBatchSize": 1000, "sessionIdleTimeout": "00:01:00", "enableCrossEntityTransactions": false } } }
欲了解更多信息,请参阅此MICROSOFT DOCUMENTATION FAQ .
关于azure - 无法从 Function App 向 Azure 服务总线发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73168561/