我想使用 Azure Functions 根据队列消息调用 REST 终结点。 Documentation告诉我...
The Functions runtime receives a message in PeekLock mode and calls Complete on the message if the function finishes successfully, or calls Abandon if the function fails.
因此,当 REST 调用失败时,我尝试通过引发主机放弃消息的异常来使该函数失败。
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
public static void Run(BrokeredMessage message, TraceWriter log)
{
string body = message.GetBody<string>();
using (var client = new HttpClient())
{
var content = new StringContent(body, Encoding.UTF8, "application/json");
var response = client.PutAsync("http://some-rest-endpoint.url/api", content).Result;
if (!response.IsSuccessStatusCode)
{
throw new Exception("Message could not be sent");
}
}
}
有人知道更好的方法来优雅地失败该函数吗?
最佳答案
记录失败并手动调用message.Abandon()
public static async Task RunAsync(BrokeredMessage message, TraceWriter log) {
var body = message.GetBody<string>();
using (var client = new HttpClient()) {
var content = new StringContent(body, Encoding.UTF8, "application/json");
var response = await client.PutAsync("http://some-rest-endpoint.url/api", content);
if (!response.IsSuccessStatusCode) {
log.Warning("Message could not be sent");
await message.AbandonAsync();
}
}
}
关于c# - 如何优雅地使服务总线触发 Azure Functions 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39935491/