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

Using Refit with ASP.Net Core 2.1's HttpClientFactory #491

Closed
thecontrarycat opened this issue May 30, 2018 · 8 comments
Closed

Using Refit with ASP.Net Core 2.1's HttpClientFactory #491

thecontrarycat opened this issue May 30, 2018 · 8 comments
Labels

Comments

@thecontrarycat
Copy link
Contributor

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 singleton HttpClient instances around. Instead, you can get as many instances of HttpClient as you like from the factory, and the underlying HttpClientHandler will have their lifecycles managed internally. This ensures that the issues with HttpClient 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 with RestService.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 the IRequestBuilder separate from the generated class, enabling us to provide a new overload to RestService.For<T>(HttpClient, IRequestBuilder), where the IRequestBuilder can be created by the caller (using RequestBuilder.For<T>() and kept around as a singleton.

I have the PR ready to go if you would like this feature.

@thecontrarycat
Copy link
Contributor Author

Working commit: thecontrarycat@bf8b0f9

@bennor
Copy link
Contributor

bennor commented Jun 1, 2018

I think this is a good idea in theory, but do you have any measurements for the performance improvement?

@thecontrarycat
Copy link
Contributor Author

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 :)

@bennor
Copy link
Contributor

bennor commented Jun 2, 2018

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.)

@bennor
Copy link
Contributor

bennor commented Jun 2, 2018

Other than a few minor nitpicks, the working commit looks good. I'd be happy to take the PR. Thoughts, @onovotny?

@clairernovotny
Copy link
Member

Certainly happy to take a PR to utilize IHttpClientFactory

@thecontrarycat
Copy link
Contributor Author

#494

@clairernovotny
Copy link
Member

Implemented, thanks!

@lock lock bot added the outdated label Jun 25, 2019
@lock lock bot locked and limited conversation to collaborators Jun 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants