Ejemplo n.º 1
0
        private static HttpHandlerContext CalculateHandlerContext(GrpcChannelOptions channelOptions)
        {
            if (channelOptions.HttpHandler == null)
            {
                // No way to know what handler a HttpClient is using so assume custom.
                var type = channelOptions.HttpClient == null
                    ? HttpHandlerType.SocketsHttpHandler
                    : HttpHandlerType.Custom;

                return(new HttpHandlerContext(type));
            }

            if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.WinHttpHandler"))
            {
                return(new HttpHandlerContext(HttpHandlerType.WinHttpHandler));
            }
            if (HttpRequestHelpers.HasHttpHandlerType(channelOptions.HttpHandler, "System.Net.Http.SocketsHttpHandler"))
            {
                HttpHandlerType type;
                TimeSpan?       connectTimeout;

#if NET5_0_OR_GREATER
                var socketsHttpHandler = HttpRequestHelpers.GetHttpHandlerType <SocketsHttpHandler>(channelOptions.HttpHandler) !;

                type           = HttpHandlerType.SocketsHttpHandler;
                connectTimeout = socketsHttpHandler.ConnectTimeout;

                // Check if the SocketsHttpHandler is being shared by channels.
                // It has already been setup by another channel (i.e. ConnectCallback is set) then
                // additional channels can use advanced connectivity features.
                if (!BalancerHttpHandler.IsSocketsHttpHandlerSetup(socketsHttpHandler))
                {
                    // Someone has already configured the handler callback.
                    // This channel can't support advanced connectivity features.
                    if (socketsHttpHandler.ConnectCallback != null)
                    {
                        type           = HttpHandlerType.Custom;
                        connectTimeout = null;
                    }
                }
#else
                type           = HttpHandlerType.SocketsHttpHandler;
                connectTimeout = null;
#endif
                return(new HttpHandlerContext(type, connectTimeout));
            }
            if (HttpRequestHelpers.GetHttpHandlerType <HttpClientHandler>(channelOptions.HttpHandler) != null)
            {
                return(new HttpHandlerContext(HttpHandlerType.HttpClientHandler));
            }

            return(new HttpHandlerContext(HttpHandlerType.Custom));
        }
Ejemplo n.º 2
0
        private bool Http2NotSupported()
        {
            if (Environment.Version.Major == 4 &&
                Environment.Version.Minor == 0 &&
                Environment.Version.Build == 30319 &&
                InnerHandler != null &&
                HttpRequestHelpers.HasHttpHandlerType <HttpClientHandler>(InnerHandler))
            {
                // https://docs.microsoft.com/dotnet/api/system.environment.version#remarks
                // Detect runtimes between .NET 4.5 and .NET Core 2.1
                // The default HttpClientHandler doesn't support HTTP/2.
                return(true);
            }

            return(false);
        }