Typed Clients
Since version 5.1 RestClient has first class support for typed clients - the same idea as ASP.NET Core’s Typed Clients (AddHttpClient<TClient>()) - letting you create maintainable SDK style clients for your domain with all the IHttpClientFactory goodness (handler pooling, lifetime management etc.) for free.
Your typed client
Your typed client just takes an IRestClient in its constructor, there is no HttpClient plumbing:
using DalSoft.RestClient;
public class GitHubClient
{
private readonly IRestClient _restClient;
public GitHubClient(IRestClient restClient)
{
_restClient = restClient;
}
public async Task<List<Repository>> GetRepositories(string user)
{
return await _restClient.Resource($"users/{user}/repos").Get<List<Repository>>();
}
public async Task<Repository> GetRepository(string user, string repository)
{
return await _restClient.Resource($"repos/{user}/{repository}").Get<Repository>();
}
}
Configuring DI
Register the typed client with AddRestClient<TClient>() passing the base uri and any default headers. It returns the same configuration object as AddRestClient(), so you can chain any of the Use*Handler() extensions and they apply to this client only:
using DalSoft.RestClient.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddRestClient<GitHubClient>("https://api.github.com", new Headers(new { UserAgent = "MyClient" }))
.UseRetryHandler();
Or if you are using Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services
.AddRestClient<GitHubClient>("https://api.github.com", new Headers(new { UserAgent = "MyClient" }))
.UseRetryHandler();
}
To register an interface with its implementation use AddRestClient<TClient, TImplementation>():
builder.Services.AddRestClient<IGitHubClient, GitHubClient>("https://api.github.com");
Using your typed client
Inject your typed client wherever you need it - typed clients are transient (exactly like AddHttpClient<TClient>()) and the underlying HttpClient is pooled by IHttpClientFactory:
public class GitHubController : Controller
{
private readonly GitHubClient _gitHubClient;
public GitHubController(GitHubClient gitHubClient)
{
_gitHubClient = gitHubClient;
}
[Route("github/users/dalsoft"), HttpGet]
public async Task<List<Repository>> GetRepositories()
{
return await _gitHubClient.GetRepositories("dalsoft");
}
}
Your typed client can take other dependencies alongside IRestClient - they are resolved from the container as usual:
public GitHubClient(IRestClient restClient, ILogger<GitHubClient> logger, IMemoryCache cache) { ... }
Taking IRestClient rather than RestClient keeps your typed client testable, and because Resource(), Get<T>(), Headers() etc. are all on the interface you lose nothing - including the underlying HttpClient, see Passthrough HttpClient. To unit test a typed client pass a RestClient configured with the UnitTestHandler - no mocking required.
Without DI
If you are not using dependency injection just new up a RestClient and pass it to your typed client - never wrap an HttpClient yourself:
var gitHubClient = new GitHubClient(new RestClient("https://api.github.com", new Headers(new { UserAgent = "MyClient" })));
If you don’t need a typed client, IRestClientFactory is the simplest way to use RestClient with IHttpClientFactory. Typed clients also combine with the McpHandler to give you an SDK for an MCP server.