Dev.ExternalClients 0.1.0

dotnet add package Dev.ExternalClients --version 0.1.0
                    
NuGet\Install-Package Dev.ExternalClients -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dev.ExternalClients" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dev.ExternalClients" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Dev.ExternalClients" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dev.ExternalClients --version 0.1.0
                    
#r "nuget: Dev.ExternalClients, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Dev.ExternalClients@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dev.ExternalClients&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Dev.ExternalClients&version=0.1.0
                    
Install as a Cake Tool

external-clients

A .NET 9.0 library that provides configurable external HTTP client support. The library enables you to register and configure external HTTP clients via dependency injection (DI). You can configure clients either by providing an inline action or by binding to a section in your configuration (e.g., appsettings.json).

Project Structure

  • src/Dev.ExternalClients/
    Contains the main library code:

    • ExternalClientOptions.cs – Defines configuration options for an external HTTP client, including properties for BaseAddress, Timeout, and DefaultRequestHeaders, and an ApplyTo method to configure an HttpClient.
    • ExternalClientServiceCollectionExtensions.cs – Provides extension methods to register external HTTP clients with DI. Supports inline configuration via an action and configuration binding from an IConfiguration instance.
  • tests/Dev.ExternalClients.Tests.Unit/
    Contains unit tests that:

    • Verify that ExternalClientOptions correctly configures an HttpClient.
    • Test various scenarios such as action-based configuration, configuration-based binding, multiple registrations, and different client setups.
  • Dev.ExternalClients.sln
    The Visual Studio solution file which includes the library and tests.

Building the Project

Make sure you have the .NET 9.0 SDK installed on your Windows machine. From the repository root, run:

dotnet build

Running the Tests

You can run unit tests by executing:

dotnet test

Usage Examples

1. Inline Action Configuration

Register an external HTTP client with inline configuration in your application startup:

services.AddExternalClient<ITestClient, TestClient>(options =>
{
    options.BaseAddress = "https://api.example.com";
    options.Timeout = TimeSpan.FromSeconds(5);
    options.DefaultRequestHeaders = new Dictionary<string, string>
    {
        { "Authorization", "Bearer token" },
        { "Accept", "application/json" }
    };
});

2. Binding from appsettings.json

Provide configuration in your appsettings.json and bind to a client options instance:

appsettings.json

{
  "TestClient": {
    "BaseAddress": "https://api.example.com",
    "Timeout": "00:00:05",
    "DefaultRequestHeaders": {
      "Authorization": "Bearer token",
      "Accept": "application/json"
    }
  }
}

Program.cs / Startup.cs

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Dev.ExternalClients;

var builder = WebApplication.CreateBuilder(args);

// Bind the TestClient section from appsettings.json to ExternalClientOptions.
builder.Services.Configure<ExternalClientOptions>(
    builder.Configuration.GetSection("TestClient")
);

// Register the client using configuration binding.
builder.Services.AddExternalClient<ITestClient, TestClient>(
    builder.Configuration
);

var app = builder.Build();
app.Run();
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 187 2/23/2025