Refit.Reflection
15.2.0
Prefix Reserved
Requires NuGet 2.12 or higher.
dotnet add package Refit.Reflection --version 15.2.0
NuGet\Install-Package Refit.Reflection -Version 15.2.0
<PackageReference Include="Refit.Reflection" Version="15.2.0" />
<PackageVersion Include="Refit.Reflection" Version="15.2.0" />
<PackageReference Include="Refit.Reflection" />
paket add Refit.Reflection --version 15.2.0
#r "nuget: Refit.Reflection, 15.2.0"
#:package Refit.Reflection@15.2.0
#addin nuget:?package=Refit.Reflection&version=15.2.0
#tool nuget:?package=Refit.Reflection&version=15.2.0
Refit: The automatic type-safe REST library for modern .NET
| Refit | Refit.HttpClientFactory | Refit.Newtonsoft.Json | Refit.Testing | |
|---|---|---|---|---|
| NuGet |
Refit is a library heavily inspired by Square's Retrofit library, and it turns your REST API into a live interface:
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<User> GetUser(string user);
}
The RestService class generates an implementation of IGitHubApi that uses
HttpClient to make its calls:
var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");
var octocat = await gitHubApi.GetUser("octocat");
.NET supports registering Refit clients via HttpClientFactory:
services
.AddRefitClient<IGitHubApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.github.com"));
To test the clients you build with Refit, the Refit.Testing package
lets you stub responses and verify requests with a declarative route table — see
Testing your Refit clients.
Table of Contents
- Sponsors
- Where does this work?
- SDK Requirements
- Breaking changes and release notes
- Source generation
- API Attributes
- Querystrings
- Body content
- Setting request headers
- Passing state into DelegatingHandlers
- Per-request timeouts
- Multipart uploads
- Retrieving the response
- Using generic interfaces
- Interface inheritance
- Default Interface Methods
- Using HttpClientFactory
- Providing a custom HttpClient
- Handling exceptions
- When returning Task<IApiResponse>, Task<IApiResponse<T>>, or Task<ApiResponse<T>>
- When returning Task<T>
- Inspecting the error body synchronously
- Reading the request body that was sent
- Providing a custom ExceptionFactory
- Providing a custom TransportExceptionFactory
- ApiException deconstruction with Serilog
- Testing your Refit clients
Sponsors
Refit is sponsored by the following:
Where does this work?
Refit currently supports the following platforms and modern .NET targets:
- WinUI
- Desktop .NET Framework 4.6.2+
- .NET 8 / 9 / 10 / 11
- Blazor
- Uno Platform
SDK Requirements
The source generator ships inside the Refit package as a Roslyn analyzer, so it is your build tools,
not your target framework, that decide whether you get generated clients:
| Requirement | Minimum |
|---|---|
| Roslyn (C# compiler) | 4.8 |
| Visual Studio |