The TaskHub.Shared.Networking module allows you to define and configure HTTP clients declaratively in appsettings.json, with built-in resilience using Polly.
Represents a single external or internal service. Each node can have its own BaseUrl and resilience policies.
Define your services in the Networking section.
"Networking": {
"Defaults": {
"RetryCount": 3,
"TimeoutInMilliseconds": 5000,
"RetryDelayInMilliseconds": 100,
"RetryStrategy": "Exponential"
},
"Services": {
"PaymentService": {
"BaseUrl": "https://payments.taskhub.com",
"RetryCount": 5,
"TimeoutInMilliseconds": 10000
},
"Nominatim": {
"BaseUrl": "https://nominatim.openstreetmap.org",
"UserAgent": "TaskHub-App"
}
}
}
Register the networking settings in your Program.cs.
using TaskHub.Shared.Networking.Implementation.Bootstrap;
builder.Services.AddAppNetworkingSettings(builder.Configuration.GetSection("Networking").Bind);
This will automatically register named HttpClients for each service defined in the Services section.
Inject IHttpClientFactory to get a configured client.
public class PaymentClient(IHttpClientFactory factory)
{
private readonly HttpClient _client = factory.CreateClient("PaymentService");
public async Task<Result> ProcessPaymentAsync(PaymentRequest req, CancellationToken ct)
{
var response = await _client.PostAsJsonAsync("/pay", req, ct);
// The client already has retries and timeouts applied!
return response.IsSuccessStatusCode ? ResultFactory.OnSuccess() : ResultFactory.OnFailed();
}
}
appsettings.json).