- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力使用在 Azure Function Http 端点中执行的 Azure ServiceBusSender。大约一天左右效果很好。然后突然它会抛出以下错误,我将不得不重新启动azure功能:
System.Exception:连接尝试失败,因为连接方在一段时间后没有正确响应,或者由于连接的主机未能响应而建立的连接失败。错误代码:超时(服务通信问题)。有关故障排除信息,请参阅 https://aka.ms/azsdk/net/servicebus/exceptions/troubleshoot。
如果我重新启动azure功能,它将继续工作大约一天。
利用 Azure.Messaging.ServiceBus 7.16.1
依赖注入(inject):
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;
}
尝试更新软件包。从函数中删除了各种额外的细节。但问题仍然存在。即使请求很少,一天后也会停止工作。不到二十。
最佳答案
我在我的环境中尝试了您的代码,并得到了以下相同的错误。
我对您的代码进行了一些更改,并收到了发送到 Azure 门户中服务总线队列的消息。
代码:
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.ServiceBus;
using System.Threading.Tasks;
[assembly: FunctionsStartup(typeof(ServiceBusFunc.Startup))]
namespace ServiceBusFunc
{
public class Startup : FunctionsStartup
{
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
public override void Configure(IFunctionsHostBuilder builder)
{
IConfiguration config = builder.GetContext().Configuration;
builder.Services.AddSingleton(config);
string serviceBusConnectionString = config.GetValue<string>("ServiceBusConnectionString");
builder.Services.AddSingleton(x => new ServiceBusClient(serviceBusConnectionString));
builder.Services.AddSingleton<ServiceBusSender>(x =>
{
var client = x.GetRequiredService<ServiceBusClient>();
string queueName = config.GetValue<string>("QueueName");
return client.CreateSender(queueName);
});
}
}
public static class SMSActivityStopGetHttpListener
{
[FunctionName("SMSActivityStopGetHttpListenerV1")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/smsactivity/stop")] HttpRequestMessage req,
[ServiceBus("<your_queue_name>", Connection = "ServiceBusConnectionString")] IAsyncCollector<ServiceBusMessage> outputMessages,
ILogger log)
{
string messageContent = "hello kamali, how are you?"; // Replace with your message content
try
{
var message = new ServiceBusMessage(messageContent);
var sender = outputMessages as IAsyncCollector<ServiceBusMessage>;
int maxRetries = 3;
TimeSpan delayBetweenRetries = TimeSpan.FromSeconds(5);
for (int retryCount = 0; retryCount < maxRetries; retryCount++)
{
try
{
await sender.AddAsync(message);
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.ServiceCommunicationProblem)
{
log.LogWarning($"Service Bus communication problem. Retrying in {delayBetweenRetries.TotalSeconds} seconds.");
await Task.Delay(delayBetweenRetries);
}
catch (Exception ex)
{
log.LogError(ex, "Error sending message to Service Bus");
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Internal Server Error", Encoding.UTF8, "application/json")
};
}
}
log.LogError("Failed to send message after multiple retries.");
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Internal Server Error - Message delivery failed", Encoding.UTF8, "application/json")
};
}
catch (Exception ex)
{
log.LogError(ex, "Error processing message");
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Internal Server Error", Encoding.UTF8, "application/json")
};
}
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ServiceBusConnectionString": "<ServiceBus_Queue_Connection_String>",
"QueueName": "<queue_name>"
}
}
注意:在运行上述代码之前,请确保您有稳定的互联网连接。
输出:
它成功运行并向服务总线队列发送一条消息。
下面是浏览器的输出:
Azure 门户:
我在 Azure 门户的服务总线队列中收到如下消息:
关于c# - ServiceBusSender 在一天后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77220203/
我知道有一些关于这个主题的类似帖子,但没有一个对我有太大帮助(或者也许这就是因为我仍然是新手)。 无论如何,我正在寻找的是一个脚本来显示日期和日期以及从现在开始的 11 天。 下面的脚本是我用来显示当
我在 mysql 表中有一个 postdate 和 status 列。目前帖子的状态是= 1。我只想在提交帖子15天后更改状态= 0。任何人都可以提供帮助。 $todays = date('d-m-Y
我有这段代码,可以将当前日期转换为这种格式 2020-08-20 。但是我如何更改它以给出今天之后 10 天以及今天之前 10 天的日期。 例如今天是2020-08-20我试图从今天起计算10天202
我刚刚在 5 天前为我的应用上传了第 12 次更新 (APK),但它在 Google Play 中仍然不可用。通常只需要几个小时就可以使用。是否有任何解决方案/原因可能会发生这种情况?以前有人经历过吗
我已经使用 React Native 创建了一个适用于 iOS 的应用程序,但是在通过 Xcode 在实际 iPhone 上安装该应用程序几天后,它只是在我启动该应用程序 0.3 秒后退出该应用程序。
几周以来,我的 iOS 应用程序一直在运行 firebase 报告。我在 2 天前按照官方步骤添加了 crashlytics: https://firebase.google.com/docs/cra
在 javascript 中,我获取当前时刻,并向其添加 3 天。为什么年份加上 3 天仍然是 2018 年,格式为 2019 年? 当从 2 天 11 小时到 3 天 10 小时的范围内添加时,这会
我 4 天前在 google play 上发布了我的第一个应用程序(语音转短信),但我仍然无法通过它的名称或开发者名称在搜索中找到我的应用程序。我只能通过包名称或真实应用名称但没有空格 - “Voic
我在我的 Activity 中创建了一个 DatePickerDialog,我试图将 minDate 设置为 2 天后。 例如,今天是 25/5/2015,我希望最小日期为:27/5/2015 这是代
jQuery(document).ready(function(){ if (document.cookie.indexOf('visited=true') === -1) {
我是 Azure 存储帐户的新用户。在学习时我发现了以下链接: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-s
我的应用在 http://talkwithstranger.com/ 上运行我已经将它部署在 AWS EC2 上。我用这个命令 sudo nohup node index.js & 即使我关闭终端并退
我是一名优秀的程序员,十分优秀!