我正在使用在Azure Function Http端点中执行的Azure服务总线。大约一天都能正常工作。然后突然它会抛出以下错误,我将不得不重新启动azure函数:System.Exception: 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. ErrorCode: TimedOut (ServiceCommunicationProblem). For troubleshooting information, see https://aka.ms/azsdk/net/servicebus/exceptions/troubleshoot.
如果我重新启动Azure功能,它将继续工作大约一天。
使用Azure.Messaging.ServiceBus 7.16.1
依赖性注射:
builder.Services.AddAzureClients(clientsBuilder =>
{
clientsBuilder.AddServiceBusClient(appConfig.GetValue<string>("CRMSBActivityConnectionString"))
.ConfigureOptions(options =>
{
options.RetryOptions.Delay = TimeSpan.FromMilliseconds(50);
options.RetryOptions.MaxDelay = TimeSpan.FromSeconds(5);
options.RetryOptions.MaxRetries = 3;
});
clientsBuilder.AddClient<ServiceBusSender, ServiceBusClientOptions>((_, _, provider) =>
provider.GetService<ServiceBusClient>().CreateSender(appConfig.GetValue<string>("CRMSBActivityTopicName")
)).WithName("SBSender");
});
Http端点:
private IServiceLogger _logger;
private const string _commonMessage = "SMSActivityStopGetHttpListenerV1:";
private readonly ServiceBusSender _sender;
public SMSActivityStopGetHttpListener(IServiceLogger logger, IAzureClientFactory<ServiceBusSender> serviceBusSenderFactory)
{
_logger = logger;
_sender = serviceBusSenderFactory.CreateClient("SBSender");
}
[ProducesResponseType((int)HttpStatusCode.Accepted)]
[ProducesResponseType((int)HttpStatusCode.InternalServerError)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
[FunctionName("SMSActivityStopGetHttpListenerV1")]
public async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/smsactivity/stop")] HttpRequest req,
ILogger log)
{
//Simple logic
try
{
await _sender.SendMessageAsync(servicebusMessage);
}
catch (Exception ex)
{
response = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(errorMessage, Encoding.UTF8, "application/json") };
SplunkLogger.LogExceptionToSplunk(_logger, _commonMessage, null, correlationId, "SMSActivityStop API failed", ex.Message);
}
return response;
}
尝试更新软件包。从函数中删除了各种额外的细节。但问题依然存在。一天后停止工作,即使很少的要求。不到二十。
2条答案
按热度按时间svmlkihl1#
我在我的环境中尝试了你的代码,得到了下面同样的错误。
编码:
local.settings.json:
输出:
它成功运行并向服务总线队列发送消息。
下面是浏览器的输出:
Azure Portal:
6qfn3psc2#
可以使用注入到函数
IAzureClientFactory<ServicBusCient>
工厂对象中的方法创建所有接收器和接收器。这个对象作为一个单例,以避免重新建立到名称空间的连接,因为这是一个昂贵的操作。发送者和接收者使用ServiceBusClient
维护的连接,并且按需创建和处理成本很低。