Skip to content

Commit fef121c

Browse files
Add readme for the DI package and add one more extensions (#2320)
1 parent 6f670e1 commit fef121c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# About
2+
3+
The `RestSharp.Extensions.DependencyInjection` library provides integration with `Microsoft.Extensions.DependencyInjection` components as well as integrates with `IHttpClientFactory`.
4+
5+
# How to use
6+
7+
Use the extension method provided by the package to configure the client:
8+
9+
```csharp
10+
// Add a default client with no base URL, with default options
11+
services.AddRestClient();
12+
13+
// Add a client with a base URL
14+
services.AddRestClient(new Uri("https://example.com"));
15+
16+
// Add a client with a base URL and custom options
17+
services.AddRestClient(options =>
18+
{
19+
options.BaseUrl = new Uri("https://example.com");
20+
options.Timeout = TimeSpan.FromSeconds(30);
21+
});
22+
```
23+
24+
When the above registrations are used, the `IRestClient` interface can be injected into any class.
25+
26+
In addition, the package supports registering named clients:
27+
28+
```csharp
29+
services.AddRestClient("my-client", options =>
30+
{
31+
options.BaseUrl = new Uri("https://example.com");
32+
options.Timeout = TimeSpan.FromSeconds(30);
33+
});
34+
```
35+
36+
When the above registrations are used, resolving the client instance should be done using the `IRestClientFactory`:
37+
38+
```csharp
39+
public class MyClass(IRestClientFactory restClientFactory)
40+
{
41+
IRestClient client = restClientFactory.CreateClient("my-client");
42+
43+
// Use the client in your code
44+
}
45+
```

src/RestSharp.Extensions.DependencyInjection/RestSharp.Extensions.DependencyInjection.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
<ItemGroup>
66
<ProjectReference Include="..\RestSharp\RestSharp.csproj" />
77
</ItemGroup>
8+
<ItemGroup>
9+
<None Update="README.md">
10+
<Pack>true</Pack>
11+
<PackagePath>/</PackagePath>
12+
</None>
13+
</ItemGroup>
814
</Project>

src/RestSharp.Extensions.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public void AddRestClient(RestClientOptions options) {
7777
services.AddRestClient(Constants.DefaultRestClient, o => o.CopyFrom(options));
7878
}
7979

80+
/// <summary>
81+
/// Adds a RestClient to the service collection with custom options.
82+
/// </summary>
83+
/// <param name="configureRestClient">Function to configure the RestClient options.</param>
84+
[PublicAPI]
85+
public void AddRestClient(ConfigureRestClient configureRestClient) {
86+
Ensure.NotNull(configureRestClient, nameof(configureRestClient));
87+
services.AddRestClient(Constants.DefaultRestClient, configureRestClient);
88+
}
89+
8090
/// <summary>
8191
/// Adds a named RestClient to the service collection with base URL.
8292
/// </summary>

0 commit comments

Comments
 (0)