複数のマルチスレッド コンシューマ間で .net HTTPClient を使用して、毎秒 127.0.0.1 でローカル サービスに GetAsync Web リクエストを作成しています。
Web リクエストは 99.9% の確率で完了しますが、場合によっては (3 ~ 4 時間にわたって) いくつかのリクエストが GetAsyc でスタックし、完了しないかタイムアウトになります。同じ期間の同じサービス URL/ポートへのリクエストは正常に機能し、新しいリクエストは正常に完了します。
GetAsync は、完了時にコールバックが呼び出されて結果の解析済みデータを処理する、ファイア アンド フォーゲット モードで起動されています (非同期を使用しない古いコードと統合されているため)。
public void Execute(Action<IAsyncCommand> onCompletion)
{
this.onAsyncCompletion = onCompletion;
try
{
// do not await as this is fire and forget
this.HandlRequestAysnc(this.Target, new StringContent(this.CommandPayload));
return;
}
catch(Exception e)
{
//log exception
}
}
private async Task HandlRequestAysnc(Uri uri, StringContent stringContent)
{
try
{
ConfiguredTaskAwaitable<HttpResponseMessage> request = stringContent != null ? webClient.PostAsync(uri, stringContent).ConfigureAwait(false) : webClient.GetAsync(uri).ConfigureAwait(false);
//this will never return or timeout 1 in 10000 times
using (HttpResponseMessage response = await request)
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
//handle result
}
}
else
{
//handle failure
}
}
}
catch (Exception ex)
{
//log exception
}
if (this.onAsyncCompletion != null)
{
this.onAsyncCompletion(this);
}
}