RS.HttpClientFactoryService 1.0.0

dotnet add package RS.HttpClientFactoryService --version 1.0.0
                    
NuGet\Install-Package RS.HttpClientFactoryService -Version 1.0.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="RS.HttpClientFactoryService" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RS.HttpClientFactoryService" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RS.HttpClientFactoryService" />
                    
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 RS.HttpClientFactoryService --version 1.0.0
                    
#r "nuget: RS.HttpClientFactoryService, 1.0.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 RS.HttpClientFactoryService@1.0.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=RS.HttpClientFactoryService&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RS.HttpClientFactoryService&version=1.0.0
                    
Install as a Cake Tool

HttpClientFactoryService

A reusable, flexible HTTP client service for .NET, built on top of IHttpClientFactory.

Supports JSON, form data, multipart requests, GET/POST/PUT/PATCH/DELETE operations, and optional CancellationToken.


## Features

- Simplified HTTP calls with minimal code

- Named HttpClient support

- Automatic JSON serialization/deserialization

- Form URL-encoded and multipart requests

- Optional headers and authorization tokens

- Handles response errors and returns structured HttpResult<T>

- Supports cancellation via CancellationToken

- Works with .NET 8+


## Installation

Install via NuGet (example package name RS.HttpClientFactoryService):


dotnet add package RS.HttpClientFactoryService


## Usage

### Register in Program.cs / Startup.cs


builder.Services.AddHttpClient();
OR
builder.Services.AddHttpClient("JsonPlaceholder", client =>
{
    client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
    client.DefaultRequestHeaders.Add("Accept", "application/json");
});

builder.Services.AddScoped<IHttpClientFactoryService, HttpClientFactoryService>();

### Define a model


public class Post

{

public int UserId { get; set; }

public int Id { get; set; }

public string Title { get; set; }

public string Body { get; set; }

}


### Basic GET Example


public class PostsController : ControllerBase

{

private readonly IHttpClientFactoryService _httpService;



public PostsController(IHttpClientFactoryService httpService)

{

    _httpService = httpService;

}



[HttpGet("posts")]

public async Task<IActionResult> GetPosts(CancellationToken cancellationToken)

{

    var result = await _httpService.GetAsync<List<Post>>(

        uri: "posts",

        clientName: "JsonPlaceholder",

        cancellationToken: cancellationToken

    );



    return result.IsSuccess ? Ok(result.Data) : BadRequest(result.Message);

}

}


POST Example (JSON from C# model)


var newPost = new Post { UserId = 1, Title = "Hello", Body = "World" };

var postResult = await _httpService.PostAsync<Post>(

uri: "posts",

requestJsonData: JsonSerializer.Serialize(newPost),

clientName: "JsonPlaceholder",

cancellationToken: cancellationToken

);


CancellationToken Support

You can pass a CancellationToken to cancel requests:


using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var result = await _httpService.GetAsync<List<Post>>(

"posts",

clientName: "JsonPlaceholder",

cancellationToken: cts.Token

);


Advanced Features

- **Multipart POST**:


var multipartContent = new MultipartFormDataContent();

multipartContent.Add(new StringContent("John"), "name");

var response = await _httpService.PostMultipartAsync<HttpResult>(

"users/upload",

multipartContent,

clientName: "MyApi",

cancellationToken: cancellationToken

);

- **Form URL-encoded POST**:


var formData = new Dictionary<string, string>

{

{ "username", "john" },

{ "password", "secret" }

};

var result = await _httpService.PostFormAsync<HttpResult>(

"login",

formData,

clientName: "MyApi",

cancellationToken: cancellationToken

);


Why use HttpClientFactoryService?

- Reduces boilerplate code for common HTTP operations

- Supports structured responses (HttpResult<T>) with status and messages

- Works seamlessly with DI and IHttpClientFactory

- Optional cancellation support

- Consistent API for GET, POST, PUT, PATCH, DELETE, form, and multipart requests


Read more / Full Article

For detailed explanation, motivation, and examples — see the full blog post:
[RS.HttpClientFactoryService — A Cleaner Way to Call APIs in .NET]
(https://www.theravinder.com/blog/rs-httpclientfactoryservice-colon-and-hyphen-a-cleaner-way-to-call-apis-in--dot-net-10068)

## License

MIT License

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.

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
1.0.0 150 11/29/2025

Release Notes – v1.0.0

Initial release of HttpClientFactoryService

Provides a centralized and reusable wrapper around IHttpClientFactory

Supports GET, POST, PUT, PATCH, DELETE, and multipart/form-data requests

Built-in JSON serialization/deserialization

Supports custom headers and authorization tokens

Includes standardized error handling and response validation

Optional request timeout and cancellation token support

Designed for clean architecture and reusable API integration

Compatible with .NET 8+