tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

Configure SocketsHttpHandler #768

Closed satishviswanathan closed 1 year ago

satishviswanathan commented 1 year ago

Flurl Version : 3.2.4

Sharing the code how I use IFlurlClientFactory to get the FlurlClient.

var httpClient = _flurlClientFactory.Get(serviceOptions.Url); httpClient.BaseUrl = serviceOptions.Url; httpClient.HttpClient.DefaultRequestVersion = new Version(HttpRequestVersion, 0); httpClient.HttpClient.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;

I'm looking for a way on how to configure the SocketsHttpHandler to enable multiple http2 connections. Do let me know If that's possible

var socketsHandler = new SocketsHttpHandler { EnableMultipleHttp2Connections = true };

tmenier commented 1 year ago

This is a programming question, not a bug. It's a good question though, so please ask on Stack Overflow where it will have greater visibility. Thanks.

satishviswanathan commented 1 year ago

@tmenier sure will do that. Just a quick question before I open this question on stackoverflow. Flurl does support http2 right ? Just wanted to check since I haven't used it with flurl

satishviswanathan commented 1 year ago

@tmenier I got http2 working with version 4.0.0-pre4. Thank you that was helpful.

Use of SocketsHttpHandler seems to be a new feature so do you want me to check in Stack OverFlow or open as a new feature request ? Please advice

tmenier commented 1 year ago

You can create a new issue requesting the enhancement. Your timing is excellent, actually. 4.0 will make it easier to configure the underlying handler, and I'll probably need to use SocketsHttpHandler rather than HttpClientHandler on platforms that support it so that things like EnableMultipleHttp2Connections are available.

satishviswanathan commented 1 year ago

Thanks @tmenier will do that.

Currently I got it working with a global setting. It would be great if we can set this while requesting for a client from the flurl client factory since I have a use case where certain client would do http 1.1 and others do http2 calls.

FlurlHttp.GlobalSettings.FlurlClientFactory = new Http2ClientFactory(); FlurlHttp.GlobalSettings.HttpVersion = "2.0";

public class Http2ClientFactory : DefaultFlurlClientFactory { public override HttpMessageHandler CreateMessageHandler() { var sslOptions = new SslClientAuthenticationOptions { RemoteCertificateValidationCallback = delegate { return true; } };

    var socketsHandler = new SocketsHttpHandler
    {

        EnableMultipleHttp2Connections = true,
        SslOptions = sslOptions
    };

    var handler = (HttpMessageHandler)socketsHandler;

    return handler;
}

}