-
Notifications
You must be signed in to change notification settings - Fork 33
Creating and disposing http clients on each request? #11
Comments
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? |
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. |
I see what you mean. I'll take a look and try to improve the HttpClient usage. |
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. |
I just published Version 2.1.0-pre1 (prerelease). Please notify me when this version works as expected. Thank you. |
There's a new version 2.1.0-pre2, which should put the default HTTP header parameters into the HTTP client object. |
2.1.0-pre3 fixes the package to enable support for Xamarin.iOS (again) |
Seems to work. Thanks for the quick update :) |
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 ... |
The HttpClient instance is tied to the RestClient instance. Otherwise, you wouldn't be able to release the resources with a dispose. |
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) ? |
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. |
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 |
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:
The text was updated successfully, but these errors were encountered: