Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/RestSharp.Extensions.DependencyInjection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# About

The `RestSharp.Extensions.DependencyInjection` library provides integration with `Microsoft.Extensions.DependencyInjection` components as well as integrates with `IHttpClientFactory`.

# How to use

Use the extension method provided by the package to configure the client:

```csharp
// Add a default client with no base URL, with default options
services.AddRestClient();

// Add a client with a base URL
services.AddRestClient(new Uri("https://example.com"));

// Add a client with a base URL and custom options
services.AddRestClient(options =>
{
options.BaseUrl = new Uri("https://example.com");
options.Timeout = TimeSpan.FromSeconds(30);
});
```

When the above registrations are used, the `IRestClient` interface can be injected into any class.

In addition, the package supports registering named clients:

```csharp
services.AddRestClient("my-client", options =>
{
options.BaseUrl = new Uri("https://example.com");
options.Timeout = TimeSpan.FromSeconds(30);
});
```

When the above registrations are used, resolving the client instance should be done using the `IRestClientFactory`:

```csharp
public class MyClass(IRestClientFactory restClientFactory)
{
IRestClient client = restClientFactory.CreateClient("my-client");

// Use the client in your code
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<ItemGroup>
<ProjectReference Include="..\RestSharp\RestSharp.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="README.md">
<Pack>true</Pack>
<PackagePath>/</PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public void AddRestClient(RestClientOptions options) {
services.AddRestClient(Constants.DefaultRestClient, o => o.CopyFrom(options));
}

/// <summary>
/// Adds a RestClient to the service collection with custom options.
/// </summary>
/// <param name="configureRestClient">Function to configure the RestClient options.</param>
[PublicAPI]
public void AddRestClient(ConfigureRestClient configureRestClient) {
Ensure.NotNull(configureRestClient, nameof(configureRestClient));
services.AddRestClient(Constants.DefaultRestClient, configureRestClient);
}

/// <summary>
/// Adds a named RestClient to the service collection with base URL.
/// </summary>
Expand Down
Loading