GM.HttpClient
1.2.0
dotnet add package GM.HttpClient --version 1.2.0
NuGet\Install-Package GM.HttpClient -Version 1.2.0
<PackageReference Include="GM.HttpClient" Version="1.2.0" />
<PackageVersion Include="GM.HttpClient" Version="1.2.0" />
<PackageReference Include="GM.HttpClient" />
paket add GM.HttpClient --version 1.2.0
#r "nuget: GM.HttpClient, 1.2.0"
#:package GM.HttpClient@1.2.0
#addin nuget:?package=GM.HttpClient&version=1.2.0
#tool nuget:?package=GM.HttpClient&version=1.2.0
<p align="center"> <img src="https://raw.githubusercontent.com/gmetskhvarishvili/GM.HttpClient/master/icon.png" alt="GM.HttpClient" width="140" height="140" /> </p>
GM.HttpClient
A lightweight, dependency-injection-friendly HttpClient for .NET. One registration call wires a
Refit client to a typed options section and layers on a
configurable Polly resilience pipeline plus a set of
delegating handlers. Part of the GM.* family of packages. Targets net10.0.
Out of the box each client gets:
- Resilience — retry (exponential backoff), circuit breaker, timeout, and an optional fallback response.
- Authorization — optional Basic auth from configuration.
- Response caching — in-memory caching of successful
GETresponses. - Header injection — static headers applied to every request.
- Context propagation — forwards the caller's IP as
X-Client-IPwhen anHttpContextis present. - Structured logging — request/response logging with timing via
ILogger.
Install
dotnet add package GM.HttpClient
Uses the ASP.NET Core shared framework (
Microsoft.AspNetCore.App) forIHttpContextAccessor, so consumers should build on the ASP.NET Core (Microsoft.NET.Sdk.Web) SDK.
Configure
Bind options from a configuration section (shape matches GMAPIClientOptions):
{
"GitHubApi": {
"BaseUrl": "https://api.github.com",
"Headers": { "User-Agent": "my-app" },
"RetryPolicy": { "RetryCount": 3 },
"CircuitBreakerPolicy": { "FailureThreshold": 2, "BreakDuration": "00:00:30" },
"TimeoutPolicy": { "Timeout": "00:00:10" },
"FallbackPolicy": { "UseFallback": true },
"Authorization": { "UseAuthorization": false },
"Caching": { "EnableCaching": true, "CacheDuration": "00:05:00" }
}
}
Register
Define a Refit interface, then register it against a configuration section and a client name:
using GM.HttpClient;
using Refit;
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<GitHubUser> GetUserAsync(string user);
}
var section = builder.Configuration.GetSection("GitHubApi");
builder.Services.AddGMHttpClient<IGitHubApi, GMAPIClientOptions>(
configSection: section,
clientName: "GitHubApi");
Use
Inject the Refit interface anywhere — the resilience pipeline and handlers run on every call:
public class ProfileService(IGitHubApi api)
{
public Task<GitHubUser> GetAsync(string user) => api.GetUserAsync(user);
}
AddGMHttpClient also accepts optional hooks for extra registrations and custom handlers:
builder.Services.AddGMHttpClient<IGitHubApi, GMAPIClientOptions>(
section,
"GitHubApi",
additionalConfig: (services, cfg) => { /* extra options binding */ },
customHandlers: services => { /* register your own DelegatingHandlers */ });
Contributing & releases
Versioning, tags, the changelog, and publishing are automated from Conventional Commits — see CONTRIBUTING.md.
License
MIT — see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Http.Polly (>= 10.0.0)
- Polly (>= 8.7.0)
- Polly.Extensions.Http (>= 3.0.0)
- Refit (>= 14.0.1)
- Refit.HttpClientFactory (>= 14.0.1)
- Refit.Reflection (>= 14.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.