Alvecta.Foundation.Infrastructure.WebApi 11.2.108

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

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: IPlaceOrderUseCase is posted to PlaceOrder. relativeUrl is where the host maps its use cases, the prefix of the route group, relative to the base address of the HTTP client: with api/orders the request goes to api/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:

    ExecuteAsync returns The host answers The caller gets
    Task 200, 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 Done
    a 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 error member. 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 the detail and the status of a problem, and an HttpRequestException for 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 ArgumentException there, and so does an error that cannot be read from a problem (ErrorJson<TError> of Alvecta.Utils.WebApi says 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 adds ValueWrapperJsonConverter (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 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. 
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
11.2.108 51 12/3/2026
11.1.107 53 12/3/2026
11.0.106 58 12/3/2026