Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support CancellationToken in requests #27

Open
drewburlingame opened this issue Oct 8, 2021 · 0 comments
Open

support CancellationToken in requests #27

drewburlingame opened this issue Oct 8, 2021 · 0 comments

Comments

@drewburlingame
Copy link

I have a console app using this library. Querying the list of repositories for one server takes 90 seconds. I can't cancel the command because I can't pass the CancellationToken through the methods. I looked at forking this but there were too many methods for me to update. I ended up with the following workaround. It works but took my a while to determine how to do. Maybe it would even be helpful just to link to an example like this until such time CancellationToken is supported?

        MyHttpClientHandler.CancellationToken = context.CancellationToken;
        FlurlHttp.GlobalSettings.HttpClientFactory = new MyDefaultHttpClientFactory();

        public class MyDefaultHttpClientFactory : DefaultHttpClientFactory
        {
            public override HttpMessageHandler CreateMessageHandler() => (HttpMessageHandler)new MyHttpClientHandler()
            {
                AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
            };
        }

        public class MyHttpClientHandler : HttpClientHandler
        {
            internal static CancellationToken CancellationToken;

            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                // ignore the default token
                return base.SendAsync(request, CancellationToken);
            }

            protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                // ignore the default token
                return base.Send(request, CancellationToken);
            }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant