liveBook · Manning

NSwag.ApiDescription.Client

As explained in ASP.NET Core in Action, 3e

NSwag.ApiDescription.Client is a .NET package that generates client code from API descriptions, including OpenAPI descriptions.

NSwag.ApiDescription.Client generates a typed C# client from an OpenAPI description. It turns endpoint metadata into request methods and serialization code, reducing the need to construct URLs or handle JSON manually. section 11.4.1 section 11.4.3

A typical Visual Studio setup follows these steps:

  • Add an OpenAPI service reference and provide the description URL, namespace, and generated class name. section 11.4.1
  • Confirm the project contains an OpenApiReference using the NSwagCSharp code generator. section 11.4.1
  • Update NSwag.ApiDescription.Client and Microsoft.Extensions.ApiDescription.Client when Visual Studio adds outdated package versions. section 11.4.1
  • Build the project to generate the client, typically under the obj folder. section 11.4.3

Calling a generated client

Create FruitClient with the API base address and an HttpClient, then call generated methods such as FruitPOSTAsync and FruitGETAsync. The generated implementation supplies endpoint paths and serializes requests and responses. Improve awkward method names by assigning endpoint names with WithName(), which becomes the OpenAPI OperationId. section 11.4.3

Listing 11.6 Calling the API from listing 11.2 using a generated client
using Fruit;                                                 #1
 
var client = new FruitClient(                                #2
    "https://localhost:7186",                                #3
    new HttpClient());                                       #4
 
Fruit.Fruit created = await client.FruitPOSTAsync("123",     #5
    new Fruit.Fruit { Name = "Banana", Stock = 100 });       #6
Console.WriteLine($"Created {created.Name}");
 
Fruit.Fruit fetched = await client.FruitGETAsync("123");     #7
Console.WriteLine($"Fetched {fetched.Name}");

Shows the complete generated-client call flow, including construction, creating a fruit, fetching it, and printing results. section 11.4.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains project configuration, package updates, generated output, client construction, calls, serialization, and endpoint naming.
Examples
Demonstrates a FruitClient that creates and fetches a Banana through generated FruitPOSTAsync and FruitGETAsync methods.
What it assumes
Assumes an available OpenAPI document and teaches the service-reference configuration needed to generate a client.
What you can do after
Build a generated client and use it to call documented API endpoints without manually serializing requests or responses.
How it connects
Connects OpenAPI descriptions, Visual Studio service references, Microsoft.Extensions.ApiDescription.Client, HttpClient, and OperationId.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.