【问题标题】:Single Instance of HttpClient with UseDefaultCredentials具有 UseDefaultCredentials 的 HttpClient 的单个实例
【发布时间】: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


【解决方案1】:

我能够使用不同的处理程序类型来解决这个问题购买

    private static WebRequestHandler _handlerCred = new WebRequestHandler
    {
        UseDefaultCredentials = true,
        UnsafeAuthenticatedConnectionSharing = true
    };

    private static HttpClient _clientCred = new HttpClient(_handlerCred);

我在这里找到了这个答案Static HttpClient still creating TIME_WAIT tcp ports

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多