Skip to content

Commit 6174a29

Browse files
committed
Add factory docs
1 parent b9753f6 commit 6174a29

File tree

2 files changed

+76
-54
lines changed

2 files changed

+76
-54
lines changed

docs/usage.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,28 @@ First, there's `DownloadDataAsync`, which returns `Task<byte[]`. It will read th
457457

458458
For larger responses, you can use `DownloadStreamAsync` that returns `Task<Stream>`. This function allows you to open a stream reader and asynchronously stream large responses to memory or disk.
459459

460+
461+
## Reusing HttpClient
462+
463+
RestSharp uses `HttpClient` internally to make HTTP requests. It's possible to reuse the same `HttpClient` instance for multiple `RestClient` instances. This is useful when you want to share the same connection pool between multiple `RestClient` instances.
464+
465+
One way of doing it is to use `RestClient` constructors that accept an instance of `HttpClient` or `HttpMessageHandler` as an argument. Note that in that case not all the options provided via `RestClientOptions` will be used. Here is the list of options that will work:
466+
467+
- `BaseAddress` will be used to set the base address of the `HttpClient` instance if base address is not set there already.
468+
- `MaxTimeout`
469+
- `UserAgent` will be set if the `User-Agent` header is not set on the `HttpClient` instance already.
470+
- `Expect100Continue`
471+
472+
Another option is to use a simple HTTP client factory. It is a static factory, which holds previously instantiated `HttpClient` instances. It can be used to create `RestClient` instances that share the same `HttpClient` instance. The cache key is the `BaseUrl` provided in the options. When you opt-in to use the factory and don't set `BaseUrl`, the `RestClient` constructor will crash.
473+
474+
```csharp
475+
var client = new RestClient(new Uri("https://example.org/api"), useClientFactory: true);
476+
```
477+
478+
::: warning
479+
Note that the `RestClient` constructor will not reconfigure the `HttpClient` instance if it's already in the cache. Therefore, you should not try using the factory when providing different options for the same base URL.
480+
:::
481+
460482
## Blazor support
461483

462484
Inside a Blazor webassembly app, you can make requests to external API endpoints. Microsoft examples show how to do it with `HttpClient`, and it's also possible to use RestSharp for the same purpose.

docs/v107/interfaces.md

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
1-
| `IRestClient` member | Where is it now? |
2-
|:------------------------------------------------------------------------------------------------|:-----------------------------------|
3-
| `CookieContainer` | `RestClient` |
4-
| `AutomaticDecompression` | `RestClientOptions`, changed type |
5-
| `MaxRedirects` | `RestClientOptions` |
6-
| `UserAgent` | `RestClientOptions` |
7-
| `Timeout` | `RestClientOptions`, `RestRequest` |
8-
| `Authenticator` | `RestClient` |
9-
| `BaseUrl` | `RestClientOptions` |
10-
| `Encoding` | `RestClientOptions` |
11-
| `ThrowOnDeserializationError` | `RestClientOptions` |
12-
| `FailOnDeserializationError` | `RestClientOptions` |
13-
| `ThrowOnAnyError` | `RestClientOptions` |
14-
| `PreAuthenticate` | `RestClientOptions` |
15-
| `BaseHost` | `RestClientOptions` |
16-
| `AllowMultipleDefaultParametersWithSameName` | `RestClientOptions` |
17-
| `ClientCertificates` | `RestClientOptions` |
18-
| `Proxy` | `RestClientOptions` |
19-
| `CachePolicy` | `RestClientOptions`, changed type |
20-
| `FollowRedirects` | `RestClientOptions` |
21-
| `RemoteCertificateValidationCallback` | `RestClientOptions` |
22-
| `Pipelined` | Not supported |
23-
| `UnsafeAuthenticatedConnectionSharing` | Not supported |
24-
| `ConnectionGroupName` | Not supported |
25-
| `ReadWriteTimeout` | Not supported |
26-
| `UseSynchronizationContext` | Not supported |
27-
| `DefaultParameters` | `RestClient` |
28-
| `UseSerializer(Func<IRestSerializer> serializerFactory)` | `RestClient` |
29-
| `UseSerializer<T>()` | `RestClient` |
30-
| `Deserialize<T>(IRestResponse response)` | `RestClient` |
31-
| `UseUrlEncoder(Func<string, string> encoder)` | `RestClientOptions.Encode` |
32-
| `UseQueryEncoder(Func<string, Encoding, string> queryEncoder)` | `RestClientOptions.EncodeQuery` |
33-
| `ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | `RestClient` |
34-
| `ExecuteAsync<T>(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
35-
| `ExecuteAsync(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
36-
| `ExecuteAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
37-
| `ExecuteGetAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
38-
| `ExecutePostAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
39-
| `ExecuteGetAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
40-
| `ExecutePostAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
41-
| `Execute(IRestRequest request)` | Deprecated |
42-
| `Execute(IRestRequest request, Method httpMethod)` | Deprecated |
43-
| `Execute<T>(IRestRequest request)` | Deprecated |
44-
| `Execute<T>(IRestRequest request, Method httpMethod)` | Deprecated |
45-
| `DownloadData(IRestRequest request)` | Deprecated |
46-
| `ExecuteAsGet(IRestRequest request, string httpMethod)` | Deprecated |
47-
| `ExecuteAsPost(IRestRequest request, string httpMethod)` | Deprecated |
48-
| `ExecuteAsGet<T>(IRestRequest request, string httpMethod)` | Deprecated |
49-
| `ExecuteAsPost<T>(IRestRequest request, string httpMethod)` | Deprecated |
50-
| `BuildUriWithoutQueryParameters(IRestRequest request)` | Removed |
51-
| `ConfigureWebRequest(Action<HttpWebRequest> configurator)` | Removed |
52-
| `AddHandler(string contentType, Func<IDeserializer> deserializerFactory)` | Removed |
53-
| `RemoveHandler(string contentType)` | Removed |
54-
| `ClearHandlers()` | Removed |
1+
| `IRestClient` member | Where is it now? |
2+
|:------------------------------------------------------------------------------------------------|:-------------------------------------------|
3+
| `CookieContainer` | Not supported, use `RestRequest.AddCookie` |
4+
| `AutomaticDecompression` | `RestClientOptions`, changed type |
5+
| `MaxRedirects` | `RestClientOptions` |
6+
| `UserAgent` | `RestClientOptions` |
7+
| `Timeout` | `RestClientOptions`, `RestRequest` |
8+
| `Authenticator` | `RestClient` |
9+
| `BaseUrl` | `RestClientOptions` |
10+
| `Encoding` | `RestClientOptions` |
11+
| `ThrowOnDeserializationError` | `RestClientOptions` |
12+
| `FailOnDeserializationError` | `RestClientOptions` |
13+
| `ThrowOnAnyError` | `RestClientOptions` |
14+
| `PreAuthenticate` | `RestClientOptions` |
15+
| `BaseHost` | `RestClientOptions` |
16+
| `AllowMultipleDefaultParametersWithSameName` | `RestClientOptions` |
17+
| `ClientCertificates` | `RestClientOptions` |
18+
| `Proxy` | `RestClientOptions` |
19+
| `CachePolicy` | `RestClientOptions`, changed type |
20+
| `FollowRedirects` | `RestClientOptions` |
21+
| `RemoteCertificateValidationCallback` | `RestClientOptions` |
22+
| `Pipelined` | Not supported |
23+
| `UnsafeAuthenticatedConnectionSharing` | Not supported |
24+
| `ConnectionGroupName` | Not supported |
25+
| `ReadWriteTimeout` | Not supported |
26+
| `UseSynchronizationContext` | Not supported |
27+
| `DefaultParameters` | `RestClient` |
28+
| `UseSerializer(Func<IRestSerializer> serializerFactory)` | `RestClient` |
29+
| `UseSerializer<T>()` | `RestClient` |
30+
| `Deserialize<T>(IRestResponse response)` | `RestClient` |
31+
| `UseUrlEncoder(Func<string, string> encoder)` | `RestClientOptions.Encode` |
32+
| `UseQueryEncoder(Func<string, Encoding, string> queryEncoder)` | `RestClientOptions.EncodeQuery` |
33+
| `ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | `RestClient` |
34+
| `ExecuteAsync<T>(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
35+
| `ExecuteAsync(IRestRequest request, Method httpMethod, CancellationToken cancellationToken)` | Extension |
36+
| `ExecuteAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
37+
| `ExecuteGetAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
38+
| `ExecutePostAsync<T>(IRestRequest request, CancellationToken cancellationToken)` | Extension |
39+
| `ExecuteGetAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
40+
| `ExecutePostAsync(IRestRequest request, CancellationToken cancellationToken)` | Extension |
41+
| `Execute(IRestRequest request)` | Extension |
42+
| `Execute(IRestRequest request, Method httpMethod)` | Extension |
43+
| `Execute<T>(IRestRequest request)` | Extension |
44+
| `Execute<T>(IRestRequest request, Method httpMethod)` | Extension |
45+
| `DownloadData(IRestRequest request)` | Extension |
46+
| `ExecuteAsGet(IRestRequest request, string httpMethod)` | Extension |
47+
| `ExecuteAsPost(IRestRequest request, string httpMethod)` | Extension |
48+
| `ExecuteAsGet<T>(IRestRequest request, string httpMethod)` | Extension |
49+
| `ExecuteAsPost<T>(IRestRequest request, string httpMethod)` | Extension |
50+
| `BuildUriWithoutQueryParameters(IRestRequest request)` | Removed |
51+
| `ConfigureWebRequest(Action<HttpWebRequest> configurator)` | Removed |
52+
| `AddHandler(string contentType, Func<IDeserializer> deserializerFactory)` | Removed |
53+
| `RemoveHandler(string contentType)` | Removed |
54+
| `ClearHandlers()` | Removed |
5555

5656
| `IRestRequest` member | Where is it now? |
5757
|:-------------------------------------------------------------------------------------------------------|:---------------------------------|

0 commit comments

Comments
 (0)