Alvecta.Foundation.Infrastructure.WebApi
11.1.107
See the version list below for details.
dotnet add package Alvecta.Foundation.Infrastructure.WebApi --version 11.1.107
NuGet\Install-Package Alvecta.Foundation.Infrastructure.WebApi -Version 11.1.107
<PackageReference Include="Alvecta.Foundation.Infrastructure.WebApi" Version="11.1.107" />
<PackageVersion Include="Alvecta.Foundation.Infrastructure.WebApi" Version="11.1.107" />
<PackageReference Include="Alvecta.Foundation.Infrastructure.WebApi" />
paket add Alvecta.Foundation.Infrastructure.WebApi --version 11.1.107
#r "nuget: Alvecta.Foundation.Infrastructure.WebApi, 11.1.107"
#:package Alvecta.Foundation.Infrastructure.WebApi@11.1.107
#addin nuget:?package=Alvecta.Foundation.Infrastructure.WebApi&version=11.1.107
#tool nuget:?package=Alvecta.Foundation.Infrastructure.WebApi&version=11.1.107
Alvecta.Foundation.Infrastructure.WebApi
Calls a use case that another host exposes over HTTP: the client counterpart of MapUseCase
(Alvecta.Foundation.Presentation.WebApi.AspNetCore). One line registers a use-case interface as a
call to the host, and whoever depends on the interface calls it as if it ran in the same process.
It belongs to the Infrastructure layer: an outbound (driven) adapter. Dependencies:
Alvecta.Utils, Alvecta.Utils.Net, Alvecta.Utils.WebApi and Microsoft.Extensions.Http. It
does not reference Alvecta.Foundation.Application.Contracts: it reads the form of a use case from
the signature of ExecuteAsync.
What it contains
AddUseCaseClient<TUseCase>(httpClientName, relativeUrl = null), an extension method on
IServiceCollection. It registers TUseCase (transient) as an implementation that posts the
request to the host and hands back what the host answers.
The route comes from the interface name, as in the mapping:
IPlaceOrderUseCaseis posted toPlaceOrder.relativeUrlis where the host maps its use cases, the prefix of the route group, relative to the base address of the HTTP client: withapi/ordersthe request goes toapi/orders/PlaceOrder. Leave it out when the host maps them at the root.The HTTP client is a named one of
IHttpClientFactory. The application registers it, with the base address of the host and whatever else a call needs: authentication, resilience, timeouts.Every form of use case is met, the eight of
Alvecta.Foundation.Application.Contracts:ExecuteAsyncreturnsThe host answers The caller gets Task200, no body a completed call Task<TResponse>200, the response the response Task<Result<TResponse, TError>>200, the response the response a problem that carries the error the error Task<Result<Done, TError>>200, no body Donea problem that carries the error the error Each of them with a request, which is posted as JSON, or without one, which posts no body.
An error comes back as the error. The mapping answers an error with a problem (RFC 9457) that carries it in its
errormember. The client reads it from there and returns it in the result, whatever status the host chose for it, so the caller switches over the same cases the use case returned on the host.Every other failed response is thrown: an
HttpProblemException(Alvecta.Utils.Net) with thedetailand the status of a problem, and anHttpRequestExceptionfor a failure that says nothing of itself. A request that breaks a rule of its own (IValidatable) is answered by the mapping with a 400 problem without an error, so it is thrown too: a rule of the request is for its caller to keep.The interface is read where the client is registered. A type that is no use-case interface throws an
ArgumentExceptionthere, and so does an error that cannot be read from a problem (ErrorJson<TError>ofAlvecta.Utils.WebApisays which errors can), instead of failing the first call.JSON is read and written with
WebApiJson.SerializerOptions(Alvecta.Utils.WebApi): the web defaults, with a value wrapper as its bare value. A host with the default JSON options of ASP.NET Core reads and writes the same, value objects excepted: a host whose use cases carry value objects addsValueWrapperJsonConverter(Alvecta.Utils.Json) to its JSON options, as shown below. Without it the host answers a request that has a value object with a 400. A host that changed its options in another way, to another naming policy or to enums as strings for example, and this client do not understand each other.
Usage
dotnet add package Alvecta.Foundation.Infrastructure.WebApi
The host maps the use case (Alvecta.Foundation.Presentation.WebApi.AspNetCore):
var orders = app.MapGroup("/api/orders");
// POST /api/orders/PlaceOrder
orders.MapUseCase<
IPlaceOrderUseCase,
IPlaceOrderUseCase.Request,
IPlaceOrderUseCase.Response,
IPlaceOrderUseCase.Error>(
OrderAnswers.ToAnswer);
A host whose use cases carry value objects has the converter of value wrappers among its JSON options, so that it reads and writes a value object as its bare value, as the client does:
using Alvecta.Utils.Json;
builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.Converters.Add(new ValueWrapperJsonConverter()));
The application that calls it registers the client:
using Alvecta.Foundation.Infrastructure.WebApi;
builder.Services.AddHttpClient("Orders", client => client.BaseAddress = new Uri("https://orders.example.com/"));
builder.Services.AddUseCaseClient<IPlaceOrderUseCase>("Orders", "api/orders");
and calls the use case through its interface, with no word of HTTP:
public sealed class CheckoutService(IPlaceOrderUseCase placeOrder)
{
public async Task<string> CheckOutAsync(string sku, int quantity, CancellationToken cancellationToken)
{
var result = await placeOrder.ExecuteAsync(new IPlaceOrderUseCase.Request(sku, quantity), cancellationToken);
return
result switch
{
IPlaceOrderUseCase.Response response => $"Order {response.OrderId} is placed.",
IPlaceOrderUseCase.Error error => Describe(error)
};
}
}
The interface, with its request, response and error, is shared by the two sides: the host implements it and the caller depends on it, so both reference the assembly that declares it.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Alvecta.Utils (>= 11.0.101)
- Alvecta.Utils.Net (>= 11.0.101)
- Alvecta.Utils.WebApi (>= 11.0.101)
- Microsoft.Extensions.Http (>= 10.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.