Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

Creating and disposing http clients on each request? #11

Closed
ashic opened this issue Jan 9, 2015 · 13 comments
Closed

Creating and disposing http clients on each request? #11

ashic opened this issue Jan 9, 2015 · 13 comments
Assignees

Comments

@ashic
Copy link

ashic commented Jan 9, 2015

Is seems the RestClient class is creating and disposing http clients for each request. This seems to be a bad practice: http://stackoverflow.com/questions/18976042/httpclientfactory-create-vs-new-httpclient . When integrating with other clients that expect long lived instances, this is causing us some problems. Is this by design? Is there a workaround? Maintaining a singleton in a custom factory doesn't work either as clients are expected to get created with a request message:

var httpClient = HttpClientFactory.CreateClient(this, request)

@fubar-coder
Copy link
Contributor

It's (currently) by design, but I already thought about a change regarding this behavior. May I ask what the problem is that you've encountered? Maybe I can help you with this problem when you can provide a small test application?

@ashic
Copy link
Author

ashic commented Jan 9, 2015

As an example, we're trying to use Cache Cow with RestSharp.Portable. It seems that if an Accept header is added, the first request uses uncached values, but subsequent requests from the same httpclient does use the cache. However, since each http request is creating a new client, this becomes a problem.

Another example is using Cache Cow's in memory cache. Creating a new client every request means the cache of the previous client isn't used. Supporting this scenario would be awesome as we'd get automatic caching with Cache Cow without having to add any caching complexity to Restsharp.Portable.

@fubar-coder
Copy link
Contributor

I see what you mean. I'll take a look and try to improve the HttpClient usage.

@ashic
Copy link
Author

ashic commented Jan 9, 2015

Awesome...looking foreward to it. I'll look at putting in a pull request for Cache Cow's core client to be made portable...all its dependencies are.

@fubar-coder
Copy link
Contributor

I just published Version 2.1.0-pre1 (prerelease). Please notify me when this version works as expected. Thank you.

@fubar-coder fubar-coder self-assigned this Jan 9, 2015
@fubar-coder
Copy link
Contributor

There's a new version 2.1.0-pre2, which should put the default HTTP header parameters into the HTTP client object.

@fubar-coder
Copy link
Contributor

2.1.0-pre3 fixes the package to enable support for Xamarin.iOS (again)

@ashic
Copy link
Author

ashic commented Jan 13, 2015

Seems to work. Thanks for the quick update :)

@simondebaecke
Copy link

Does this means that we also have to use the same RestClient object across the app (like a singleton) instead of many instances (for each request) using disposable statement ? :)

According posts and this commit, it's what I understand but I'm not sure to be right ...

@fubar-coder
Copy link
Contributor

The HttpClient instance is tied to the RestClient instance. Otherwise, you wouldn't be able to release the resources with a dispose.
EDIT: Nevermind, I have to change the internal API to support a single HttpClient instance for multiple RedtClient instances.

@simondebaecke
Copy link

Or instead, I re-use the same RestClient instance for each request to make. This is equivalent or even better, no ?

But at the end, what is the difference of performance between re-using a single RestClient instance (or like you said, the same HttpClient from RestClient) and using different instance for each request (with using statement) ?

@fubar-coder
Copy link
Contributor

I guess that it's much better to use a single RestClient instance.

PS: Please contact me directly or in my subbreddit /r/FubarDev, when you have further questions, because this issue doesn't seem to be the right platform for this kind of discussions.

@simondebaecke
Copy link

Ok, sorry, I was really thinking my questions are related to this issue. ;)

I will contact you for shortly about a "ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength." exception when re-use of the same RestClient instance ... xD

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants