HttpMessenger 1.0.3
dotnet add package HttpMessenger --version 1.0.3
NuGet\Install-Package HttpMessenger -Version 1.0.3
<PackageReference Include="HttpMessenger" Version="1.0.3" />
paket add HttpMessenger --version 1.0.3
#r "nuget: HttpMessenger, 1.0.3"
// Install HttpMessenger as a Cake Addin #addin nuget:?package=HttpMessenger&version=1.0.3 // Install HttpMessenger as a Cake Tool #tool nuget:?package=HttpMessenger&version=1.0.3
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;
}
}
Testing example
public class ProductsControllerTests : IntegrationTest // Inject in base class
{
[Fact]
public async Task GetProducts_WithSearchParam_ShouldOnly_ReturnMatchingProducts()
{
var response =
await Messenger.Get<PaginatedList<ProductDto>>("products", new { search = "toothpaste", pageSize = 50 });
response.Data.Should().AllSatisfy(x => x.Name.Contains(searchTerm));
}
}
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 | Versions 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. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- System.Net.Http (>= 4.3.4)
- System.Net.Http.Json (>= 6.0.0)
- System.Text.Json (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.