在执行一些集成测试用例时,我在Linux OS
上遇到.Net Core 2.2
的问题。这是堆栈跟踪:
失败的mytest()错误消息:System.Net.WebException:失败 5次重试后----> System.Net.WebException:连接被拒绝 连接被拒绝----> System.Net.Http.HttpRequestException: 连接被拒绝----> System.Net.Sockets.SocketException: 连接被拒绝堆栈跟踪:WebApiClient.cs:第56行
WebApiClient.cs:第34行位于 PdsWebAppTests.ClientForTestPdsWebApp.Call(方法,字符串url, 字符串requestContent,WebClientHeaders标头) ClientForTestPdsWebApp.cs:第19行,位于 IntegrationTests.IntegrationTests1.mytest() in IntegrationTests.cs:line 44 --WebException at System.Net.HttpWebRequest.GetResponse() at WebApiClient.GetResponse(WebRequest webRequest) in WebApiClient.cs:line 117 at WebApiClient.ReceiveResponse(WebRequest webRequest) in WebApiClient.cs:line 97 CallWithRetries(Method method, String uriString, String requestContent, WebClientHeaders headers, Int32 retryCount) in WebApiClient.cs:line 46 --HttpRequestException at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask
1.get_Result()位于 System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage 的请求,在 System.Threading.Tasks.ValueTask1.get_Result() at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask
1 位于System.Threading.Tasks.ValueTask1.get_Result()
1 sendTask上的creationTask), HttpRequestMessage请求,CancellationTokenSource cts,布尔值 DisposeCts)位于System.Net.HttpWebRequest.SendRequest()处 System.Net.HttpWebRequest.GetResponse() System.Net.Http.ConnectHelper.ConnectAsync上的--SocketException(字符串主机,Int32端口, CancellationToken cancellingToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task
这是我的高级代码流:
public Response Call(Method method, string uriString, WebClientHeaders headers, string requestContent)
{
var response = CallWithRetries(method, uriString, requestContent, headers, NumberOfTimesToRetry); //WebApiClient.cs:line 34
return response;
}
private Response CallWithRetries(Method method, string uriString, string requestContent, WebClientHeaders headers, int retryCount)
{
var lastException = new WebException();
for (var retriesLeft = retryCount; retriesLeft > 0; --retriesLeft)
{
try
{
var webRequest = CreateRequest(method, uriString, requestContent, headers);
var response = ReceiveResponse(webRequest);
return response;
}
catch (WebException e)
{
lastException = e;
}
Thread.Sleep(RetryBackoffPeriod);
}
throw new WebException($"Failed after {retryCount} retries", lastException);
}
private Response ReceiveResponse(WebRequest webRequest)
{
var json = "no json returned :-(";
var webResponse = GetResponse(webRequest); //at WebApiClient.ReceiveResponse(WebRequest webRequest) in WebApiClient.cs:line 97
if (webResponse == null) return null;
using (webResponse)
{
var responseStream = webResponse.GetResponseStream();
if (responseStream != null)
{
using (var y = new StreamReader(responseStream))
{
json = y.ReadToEnd();
}
}
return new Response(((HttpWebResponse)webResponse).StatusCode, json);
}
}
P.S。
该测试用例在Windows中运行良好。同样从Linux机器上,我能够启动Web应用程序并执行所有操作,所以我认为只是WebRequest.GetResponse()
引发了异常。