-
-
Notifications
You must be signed in to change notification settings - Fork 747
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
Using Refit with ASP.Net Core 2.1's HttpClientFactory #491
Comments
Working commit: thecontrarycat@bf8b0f9 |
I think this is a good idea in theory, but do you have any measurements for the performance improvement? |
All I've done is moved the existing caching to a different place so that people who aren't using a singleton HttpClient can benefit from it in the same way that those with a singleton HttpClient currently do. I presume the existing caching was put there for a reason :) |
I'm not sure when the caching was added or why. My only real concern is that we have an outstanding bug relating to concurrency (#449), and I'm not sure if that has anything to do with the existing caching. (I haven't had time to look at it yet - it may not even exist in the latest version.) |
Other than a few minor nitpicks, the working commit looks good. I'd be happy to take the PR. Thoughts, @onovotny? |
Certainly happy to take a PR to utilize |
Implemented, thanks! |
The ASP.Net Core team have created a new way of working with HttpClient: the HttpClientFactory https://github.com/aspnet/HttpClientFactory
The idea behind
HttpClientFactory
is that you no longer need to worry about keeping singletonHttpClient
instances around. Instead, you can get as many instances ofHttpClient
as you like from the factory, and the underlyingHttpClientHandler
will have their lifecycles managed internally. This ensures that the issues withHttpClient
that added the singleton requirement such as socket exhaustion are solved, while the issues with using a singleton (such as failing to respond to DNS changes due to long-lived connections) are also handled.Currently, to use
HttpClientFactory
with Refit you can simply new up an instance of your service withRestService.For<T>(HttpClient)
. However, this can lead to a performance reduction due to the way methods are cached (in a dictionary on the generated service class).To make this work more smoothly, I propose moving the method caching out of the generated service class and adding it as a decorator around the
RequestBuilderImplementation
. This then makes theIRequestBuilder
separate from the generated class, enabling us to provide a new overload toRestService.For<T>(HttpClient, IRequestBuilder)
, where theIRequestBuilder
can be created by the caller (usingRequestBuilder.For<T>()
and kept around as a singleton.I have the PR ready to go if you would like this feature.
The text was updated successfully, but these errors were encountered: