azure - Azure Functions 失败时重试输出服务总线消息

标签 azure azure-functions

我有一个 HTTP 触发器 Azure 函数。此函数会将一些数据保存到数据库中,返回 HttpResponseData 并输出 Azure 服务总线消息。

情况是,如果函数由于任何原因(例如服务总线连接字符串配置错误)无法将消息发送到服务总线,则数据已被持久化,但服务总线中没有消息。

如何让 Azure Functions 在无法向服务总线发送消息或回滚整个事务时重试?

这是我的函数代码,.NET 6isolated。

[Function("TestFunction")]
public async Task<TestFunctionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");

    // This is to persist some data to the database
    var result = await _someService.SubmitSomeData(someData);

    // This is to create an HttpResponseData object
    var response = await req.CreateResponseAsync(result);

    return new TestFunctionResult
    {
        OutputMessage = new TestTopic("First Last", "My Company"),
        HttpResponse = response
    };
}

这是TestFunctionResult的定义

public class TestFunctionResult
{
    [ServiceBusOutput("TestTopic", Connection = "ServiceBus", EntityType = ServiceBusEntityType.Topic)]
    public TestTopic OutputMessage { get; set; }

    public HttpResponseData HttpResponse { get; set; }
}

最佳答案

我已使用您的代码重现了该问题,它对我有用。

为了重试向服务总线发送消息失败的情况,我按照 documentationhost.json 文件中添加了以下代码

host.json 文件-

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensions": {
    "serviceBus": {
      "clientRetryOptions": {
        "mode": "exponential",
        "tryTimeout": "00:01:00",
        "delay": "00:00:00.80",
        "maxDelay": "00:01:00",
        "maxRetries": 3
      }
    }
  }
}

function.cs 文件-

using System;
using System.Net;
using System.Text;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus;
    
namespace FunctionApp2
{
    public class TestTopic
    {
        public string Name { get; set; }
        public string Company { get; set; }

        public TestTopic(string name, string company)
        {
            Name = name;
            Company = company;
        }
    }

    public static class Function1
    {
        [Function("TestFunction")]
        public static async Task<TestFunctionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData reqs, FunctionContext executionContext)
        {

            var logger = executionContext.GetLogger("TestFunction");

            // This is to create an HttpResponseData object
            var response = reqs.CreateResponse(HttpStatusCode.OK);

            return new TestFunctionResult
            {
                OutputMessage = new TestTopic("First Last", "My Company"),
                HttpResponse = response
            };
        }
    }

    public class TestFunctionResult
    {
        [ServiceBusOutput("TestTopic", Connection = "ServiceBusConnection", EntityType = ServiceBusEntityType.Topic)]
        public TestTopic? OutputMessage { get; set; }
        public HttpResponseData? HttpResponse { get; set; }
    }
}

local.settings.json 文件-

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "ServiceBusConnection": "your connection string"
  }
}

输出-

enter image description here

门户- 之前-

enter image description here

之后-

enter image description here

enter image description here

关于azure - Azure Functions 失败时重试输出服务总线消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76426549/

相关文章:

azure - 如何使用https ://github. com/Azure/azure-powershell源代码?

node.js - 如何在azure应用程序服务上调试nodejs应用程序 - 获取http状态代码500

c# - Azure 功能身份验证 - 可以在没有 AD 的情况下吗?

azure - 本地运行具有 nuget 包引用的 Azure Functions 时出错

azure-functions - Azure 函数 - 从 Azure key 保管库获取服务总线连接字符串

Azure Function Web 套接字客户端

Azure 流量管理器,优先模式 : Browser refresh won't go to secondary node when primary goes down

azure - 使用Azure Function删除原始图片时如何删除缩略图?

python - 无法从 Azure 事件中心写入 CosmosDB

azure - ##[错误]本地不存在带有标签 : icndpaksacr. azurecr.io/autopipe 的图像