HttpMessenger 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package HttpMessenger --version 1.0.1                
NuGet\Install-Package HttpMessenger -Version 1.0.1                
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="HttpMessenger" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpMessenger --version 1.0.1                
#r "nuget: HttpMessenger, 1.0.1"                
#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.
// Install HttpMessenger as a Cake Addin
#addin nuget:?package=HttpMessenger&version=1.0.1

// Install HttpMessenger as a Cake Tool
#tool nuget:?package=HttpMessenger&version=1.0.1                

HttpMessenger

HttpMessenger is a service for ASP.NET Core that provides a simple generic wrapper around the HttpClient and makes it easier to make requests to an API.

Usage guide

Install the package

Package Manager Console:

Install-Package HttpMessenger

dotnet CLI:

dotnet add package HttpMessenger

Or simply get it from the NuGet Gallery.

Add the service to your app

Configure your HttpClient in the dependency injection container like you normally would.

Program.cs (.NET 6 >) example:

builder.Services.AddScoped(sp => new HttpClient 
{ 
    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress + "api/") 
});

Then add the HttpMessenger service to your Dependency Injection container

builder.Services.AddHttpMessenger();

Startup.cs (.NET 6 <) Example:

services.AddScoped(sp => new HttpClient 
{ 
    BaseAddress = new Uri("Set your base address here") 
});

Then add the HttpMessenger service to your Dependency Injection container

services.AddHttpMessenger();

Usage

The service is now available to use and can be injected into Blazor components, Controllers or any other service registered with the DI container.

Blazor component example
// Can be injected like this
@inject IHttpMessenger _httpMessenger

@code {
    // or this
    [Inject]
    private IHttpMessenger HttpMessenger { get; set; }
    
    protected override async Task OnInitializedAsync()
    {
        var response = await HttpMessenger.Get<IList<ProductDto>>("products");
        _products = response.Data;
    }
}
Controller example
public class ProductsController : Controller
{
    private readonly IHttpMessenger _httpMessenger;
    
    public ProductsController(IHttpMessenger httpMessenger)
    {
        _httpMessenger = httpMessenger;
    }
    
    [HttpGet]
    public async Task<IActionResult> Index()
    {
        var response = await _httpMessenger.Get<IList<ProductDto>>("products");
        return View(response.Data);
    }
Service example
public class ProductsService : IProductsService
{
    private readonly IHttpMessenger _httpMessenger;
    private const string Endpoint = "api/products";
    
    public ProductsService(IHttpMessenger httpMessenger)
    {
        _httpMessenger = httpMessenger;
    }
    
    public async Task<List<ProductDto>> GetProducts()
    {
        var response = await _httpMessenger.Get<List<ProductDto>>(Endpoint);
        return response.Data;
    }
    
    public async Task<ProductDto> CreateProduct(CreateProductDto product)
    {
        var response = await _httpMessenger.Post<CreateProductDto, ProductDto>(Endpoint, product);
        return response.Data;
    }
}

Query Parameters

Query parameters can be added to the request in two different ways.

The first way is to add them to the anonymous object that can be passed to the get method. Here the variable name will be serialized as the name and the value as the value.

await _messenger.Get<IList<ProductDto>>("products", new { page = 10, pageSize = 10 });

(Also supports directly passing objects, arrays or lists)

The other way is to add them to the query string manually.

await _messenger.Get<IList<ProductDto>>("products?page=1&pageSize=10");

Supported HTTP methods

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

TODO

  • Support converting more query parameters
  • More test coverage
  • IHttpClientFactory support if you want to use multiple HttpClients [maybe]

Something missing?

Feel free to open an issue or make a pull request.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.3 5,117 8/8/2022
1.0.2 1,033 5/1/2022
1.0.1 431 2/5/2022
1.0.0 405 1/30/2022