TaskHub.Shared

Resilient Networking

The TaskHub.Shared.Networking module allows you to define and configure HTTP clients declaratively in appsettings.json, with built-in resilience using Polly.

Core Concepts

NetworkNode

Represents a single external or internal service. Each node can have its own BaseUrl and resilience policies.

Resilience Policies

Configuration

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"
    }
  }
}

Registration

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.

Usage

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();
    }
}

Why declarative?

  1. Centralization: All external dependencies are documented in one place (appsettings.json).
  2. Environment Specific: You can easily change URLs or timeouts for staging/production without changing code.
  3. Consistency: All services follow the same resilience patterns by default.