在Azure Functions中失败时重试输出服务总线消息

u5rb5r59  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(94)

我有一个HTTP触发器Azure函数。此函数将一些数据保存到数据库中,返回HttpResponseData并输出Azure Service Bus消息。
这种情况是,如果函数由于任何原因(例如,错误配置的服务总线连接字符串)无法将消息发送到服务总线,则数据已经持久化,但服务总线中没有消息。
如何让Azure函数在未能向服务总线发送消息或回滚整个事务时重试?
这是我的函数代码,.NET 6 Isolated。

[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; }
}
jckbn6z7

jckbn6z71#

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

为了在无法向服务总线发送消息时重试,我按照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"
  }
}

输出-

入口-之前-

之后-

相关问题