Exemplo n.º 1
0
        /// <summary>
        /// Based on https://github.com/NimaAra/Easy.Common/blob/master/Easy.Common/RestClient.cs and
        /// the associated blog post https://nima-ara-blog.azurewebsites.net/beware-of-the-net-httpclient/
        /// </summary>
        private void AddConnectionLeaseTimeout(Uri endpoint)
        {
            if (!endpoint.IsAbsoluteUri)
            {
                return;
            }

            var key = new EndpointCacheKey(endpoint);

            if (!_endpoinsWithTcp.TryGetValue(key, out var _))
            {
                // ServicePointManager is responsible for managing different properties of a TCP connection
                // and one of such properties is the ConnectionLeaseTimeout.
                // This specifies how long (in ms) the TCP socket can stay open.
                // By default the value of this property is set to -1 resulting in the socket
                // staying open indefinitely (relatively speaking) so all we have to do is set it to a more realistic value:
                try
                {
                    ServicePointManager.FindServicePoint(endpoint)
                    .ConnectionLeaseTimeout = (int)HttpClientConfig.ConnectionLifeTime.TotalMilliseconds;
                }
                catch (NotImplementedException)
                {
                    // Unity doesn't implement see https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/issues/2537
                }
                _endpoinsWithTcp[key] = true;
            }
        }
        /// <summary>
        /// Based on https://github.com/NimaAra/Easy.Common/blob/master/Easy.Common/RestClient.cs and
        /// the associated blog post https://nima-ara-blog.azurewebsites.net/beware-of-the-net-httpclient/
        /// </summary>
        private void AddConnectionLeaseTimeout(Uri endpoint)
        {
            if (!endpoint.IsAbsoluteUri)
            {
                return;
            }

            var key = new EndpointCacheKey(endpoint);

            if (!_endpoinsWithTcp.TryGetValue(key, out var _))
            {
                // ServicePointManager is responsible for managing different properties of a TCP connection
                // and one of such properties is the ConnectionLeaseTimeout.
                // This specifies how long (in ms) the TCP socket can stay open.
                // By default the value of this property is set to -1 resulting in the socket
                // staying open indefinitely (relatively speaking) so all we have to do is set it to a more realistic value:
                ServicePointManager.FindServicePoint(endpoint)
                .ConnectionLeaseTimeout = (int)HttpClientConfig.ConnectionLifeTime.TotalMilliseconds;
                _endpoinsWithTcp[key]   = true;
            }
        }