【发布时间】:2019-12-14 17:17:49
【问题描述】:
我在 Azure 应用服务中运行的 .NET Core 2.X 应用中运行此方法。我有一个远程服务器,我们使用此方法从 Angular 网站中的按钮按下调用。调用远程设备。
Angular 按钮 --> Azure 中的 .NET Core 应用服务 --> 另一个应用服务 --> 互联网\蜂窝连接设备。我们等待设备的响应返回状态码。
如果我快速向此方法发送命令 [2 或 3 次],它会导致应用服务停止响应,直到我重新启动它。我读了这个post 并添加了[, 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);
}
}
【问题讨论】:
-
async void始终是一个危险信号,除非它是一个事件处理程序。你有什么理由不返回Task?你怎么称呼这个方法?我们能看到那个代码吗?
标签: c# asp.net-mvc .net-core async-await