【发布时间】:2018-08-23 14:58:53
【问题描述】:
我一直在尝试使用此处的建议更新我的 API 调用 只有 1 个 HttpClient 实例。 https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/#
这很好用,我没有用完所有端口,但是当我尝试创建一个 HttpClientHandler 来传递默认凭据时,我的端口又开始被用完。出于安全原因,我的 API 设置为使用 Windows Auth,因此我需要传递应用程序池凭据才能成功调用。
这是两个代码块
public static class WebApiCallUtility
{
private static HttpClientHandler _handlerNoCred = new HttpClientHandler();
private static HttpClient _clientNoCred = new HttpClient(_handlerNoCred);
private static HttpClientHandler _handlerCred = new HttpClientHandler { UseDefaultCredentials = true };
private static HttpClient _clientCred = new HttpClient(_handlerCred);
//Working ports are not used up
public static HttpResponseMessage SendHttpGetRequestNoCred(string webApiUrl, string logSourceName, string subId)
{
_clientNoCred.DefaultRequestHeaders.Accept.Clear();
_clientNoCred.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage result = _clientNoCred.GetAsync(webApiUrl).Result;
return result;
}
//No working tons of ports open hanging out with TIME_WAIT status
public static HttpResponseMessage SendHttpGetRequestCred(string webApiUrl, string logSourceName, string subId)
{
_clientCred.DefaultRequestHeaders.Accept.Clear();
_clientCred.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage result = _clientCred.GetAsync(webApiUrl).Result;
return result;
}
}
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
您能否提供一个提琴手转储或显示您有 TIME_WAITS 时发送的请求的内容?
标签: asp.net asp.net-web-api singleton httpclient