Skip to content

Beffyman/AspNetCore.Client

Repository files navigation

Beffyman.AspNetCore.Client

Build Status

If you are anything like me, you look at

using(var client = new HttpClient())
{
	var request = await client.GetAsync($"api/mysuperspecialroute/{id}");
	//Now to make sure what came back is what is expected, in every case...
}

and think the following

  • Why not just inject clients?
    • services.AddTestWebClients();!
  • How can I pool the HttpClient usage?
    • HttpClient is injected! Which allows you to control it's lifecycle
  • Yuck, hard coded routes, these can lead to issues if my endpoint is still under development.
    • Generated On Build! Beffyman.AspNetCore.Client.Generator is a before compile build task that will generate clients inside the project that implements it.
  • How do I unit test this without spinning up a full web app?
    • Works with Microsoft.AspNetCore.TestHost!
      • CancellationTokens are not respected inside the TestServer without some hacks though (registering a kill of the server) due to the Token not being checked.
  • How do I tell my teammates that an endpoint has headers it requires?
    • HeaderParameterAttribute! Which makes a header a parameter inside the generated methods, which may or may not be required.
    • IncludeHeaderAttribute! Which defines a constant header value to be included
  • How do I tell my teammates that an endpoint has known response types for status codes?
    • ProducesResponseType! Generates action parameters that allow custom logic depending on the status code returned, without needing to manually check it.
  • What if sometimes I want to intercept requests before they go out?
    • IHttpOverride! Which allows for potential cache interception of requests.
  • If I own the endpoint's code, why can't I just generate clients from it to make interacting with it as simple as injecting it?
    • Introducing Beffyman.AspNetCore.Client.Generator!

First Time Setup

Supported Frameworks

  • AspNetCore 3.1 HTTP Controllers
  • AspNetCore 3.1 SignalR Hubs
  • AspNetCore 3.1 Blazor Client Side
  • Http Trigger Azure Functions v3

Beffyman.AspNetCore.Client

NuGet

Includes ServiceCollection registration logic, used on the Client

Beffyman.AspNetCore.Client.Generator

NuGet

On Build generator that will generate a Clients.cs file based on the Properties in the csproj.

Beffyman.AspNetCore.Server

NuGet

Includes attributes that can affect generation, used on your AspNetCore api app

Beffyman.AspNetCore.Client.Protobuf

NuGet

Contains a protobuf serializer which can override the default json one via the UseProtobufSerlaizer on the ClientConfiguration.

services.AddTestWebClients(config=>
{
	config.UseProtobufSerializer()
			.UseProtobufDeserializer()
			.WithProtobufBody();
});

Beffyman.AspNetCore.Client.MessagePack

NuGet

Contains a MessagePack serializer which can override the default json one via the UseMessagePackSerializer on the ClientConfiguration.

Requires version 1.7.3.7 at the moment due to dotnet/aspnetcore#18074

services.AddTestWebClients(config=>
{
	config.UseMessagePackSerializer()
			.UseMessagePackDeserializer()
			.WithMessagePackBody();
});

Beffyman.AspNetCore.Client.NewtonsoftJson

NuGet

Contains a Newtonsoft Json serializer which can override the default json one via the UseNewtonsoftJsonHttpSerializer on the ClientConfiguration.

services.AddTestWebClients(config=>
{
	config.UseNewtonsoftJsonHttpSerializer()
			.UseNewtonsoftJsonHttpDeserializer()
			.WithJsonBody();
});

Beffyman.AspNetCore.Client.Http

NuGet

Uses Microsoft.Extensions.Http to inject a client factory. This allows for better reuse of the underlying HttpMessageHandler.

services.AddTestWebClients(config=>
{
	config.UseHttpClientFactory<ITestWebAppClient>();
});