c# - Azure .NET Core Appservice 由于异步调用而锁定发送 502.3
问题描述
我在 Azure 应用服务中运行的 .NET Core 2.X 应用中运行此方法。我有一个远程服务器,我们使用此方法从 Angular 网站中的按钮按下调用。调用远程设备。
Angular 按钮 --> Azure 中的 .NET Core 应用服务 --> 另一个应用服务 --> internet\cell 连接设备。我们等待设备的响应返回状态码。
如果我快速向此方法发送命令 [2 或 3 次],它会导致应用服务停止响应,直到我重新启动它。我阅读了这篇文章并添加了[, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)]
.
但是我仍然可以冻结整个应用程序并需要重新启动才能快速向此方法发送命令。
private async void SetEndPointValueAsync(string stunnelUrl, string username, string password)
{
try
{
//set the user name and password
var httpClientHandler = new HttpClientHandler()
{
Credentials = new NetworkCredential(username, password)
};
using (var client = new HttpClient(httpClientHandler))
{
using (var response = await client.GetAsync(stunnelUrl**, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)**)
{
if (response.IsSuccessStatusCode)
{
LogInfo(typeof(IntegrationService), stunnelUrl, LogAction.EndpointUpdate);
}
else
{
//request failed.
LogWarning(typeof(IntegrationService), stunnelUrl, LogAction.DeviceRequest);
}
//using (var content = response.Content)
//{
// //do here your changes when required
//}
}
}
}
catch (Exception e)
{
LogErrorDetailed(e, typeof(IntegrationService), stunnelUrl, LogAction.DeviceRequest);
}
}
解决方案
通常,您不想创建如此多的实例,HttpClient
因为您会失去它提供的管理带来的很多好处。
你可以通过只有一个实例来减少一些开销,就像这样......
private readonly HttpClient _client;
public ClassConstructor(HttpClient client)
{
_client = client ?? new HttpClient();
}
然后你可以改变你的方法看起来像这样......
private async Task SetEndPointValueAsync(Uri stunnelUri, string username, string password)
{
if (stunnelUri == null) throw new ArgumentNullException(nameof(stunnelUri));
if (String.IsNullOrEmpty(username)) throw new ArgumentNullException(nameof(username));
if (String.IsNullOrEmpty(password)) throw new ArgumentNullException(nameof(password));
byte[] byteArray = Encoding.ASCII.GetBytes($"{username}:{password}");
string scheme = "Basic";
string parameter = Convert.ToBase64String(byteArray);
HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.RequestUri = stunnelUri;
request.Headers.Authorization = new AuthenticationHeaderValue(scheme, parameter);
try
{
HttpResponseMessage response = await _client.SendAsync(request);
// This will throw an HttpRequestException if there is a failure above
response.EnsureSuccessStatusCode();
// do your stuff here...
// { ... }
}
catch (HttpRequestException)
{
// { log your connection / bad request issue. }
}
catch (Exception)
{
// I don't recommend this, but since you had it...
// { handle all other exceptions }
}
}
推荐阅读
- machine-learning - 在训练过程中无法使用 keras 保存最佳权重
- javascript - TabulatorJS 4.5 查找表函数:结合 deleteRow() 不起作用
- javascript - 将图像设为按钮且不可下载
- python - python pygame 背景图片随屏幕大小变化
- visual-studio - 现在在自定义目录中安装了“Microsoft VS code”我无法使用 Go 支持
- eclipse - 如何在新的 Eclipse 和 Scene Builder 之间建立链接?
- java - 类在同一个jar NPE中使用txt文件
- reactjs - React App 出现 MongoDB 错误“MustAuthenticateFirst”
- angular - Angular Material中带有菜单的完整背景图像
- android - Android 使用 Dagger2 和多个项目模块