Hexarc Pact provides a toolchain for exposing and consuming Web API for .NET/TypeScript-based projects.
Develop your .NET Web API project with Pact to keep a single source of truth for the API metadata and consume it across the .NET ecosystem (microservices, desktop and mobile applications) or on the Web via TypeScript.
| Package | Platform | Version | Downloads |
|---|---|---|---|
Hexarch.Pact.Protocol |
.NET 7.0+ | ||
Hexarch.Pact.AspNetCore |
.NET 7.0+ | ||
Hexarch.Pact.Client |
.NET 7.0+ | ||
Hexarch.Pact.Tool |
.NET 7.0+ | ||
@hexarc/pact-tool |
TypeScript |
The Pact type system is based on the .NET CLR types with some bespoke ones for the best development experience.
What's inside the Pact type system:
Guid and DateTime)The Pact API schema is designed to have an RPC-like semantic. In doing so an exposed API must follow these rules:
ApiController and Route attributes.[Route("Entities")]).HttpGet or HttpPost). Verb attributes may contain an endpoint path.
Others are not supported at the moment.
HttpGet methods can have query parameters which must be bind via the FromQuery attribute.HttpPost methods must have only one parameter which is the request body.Route attribute. If the method path is not
specified in the HTTP verb attribute it will be taken from the Route one or an exception will be raised.Pact provides some useful attributes for API annotation:
PactIgnoreAttribute can be applied to API controllers or methods to be excluded from an API schema.PactScopeAttribute can be applied to API controllers to be isolated into specific scope in an API schema.Demo API server can be found at https://hexarc-demo-api.herokuapp.com/.
The Pact API schema is exposed at https://hexarc-demo-api.herokuapp.com/pact/schema.
To generate an API client use the instruction below.
Find out how to use Pact to expose and consume a typical Web API.
Hexarc.Pact.AspNetCore package in a .NET Web API project:dotnet add package Hexarc.Pact.AspNetCoreStartup.ConfigureServices method:public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Register the Pact schema generation.
services.AddPactGeneration();
}Startup.Configure method:public void Configure(IApplicationBuilder app)
{
// Enable the Pact middleware to expose the generated API schema.
app.UsePact();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}[ApiController, Route("Misc")]
public sealed class MiscController : ControllerBase
{
[HttpGet(nameof(Ping))]
public String Ping([FromQuery] String message) => $"Hello, {message}";
}YOUR_API_HOST/pact/schema address to ensure
the Pact API schema is generated.Hexarc.Pact.Client package in a .NET project:dotnet add package Hexarc.Pact.ClientHexarc.Pact.Tool tool in the project:dotnet tool install Hexarc.Pact.ToolYou may need to setup a .NET tools manifesto before
install the Hexarc.Pact.Tool tool:
dotnet new tool-manifestpact.json config in the project with a desired API client
generation settings:{
"schemaUri": "YOUR_API_HOST/pact/schema",
"clientClassName": "DemoClient",
"clientClassNamespace": "DemoNamespace",
"outputDirectory": "Generated"
}where
schemaUri - link to a Pact API schemaclientClassName - name for a generated API client classclientClassNamespace - namespace where to put in the generated API client classoutputDirectory - output directory for the generated sourcesdotnet pactThis command must be performed in the same folder with the pact.json config.
var client = new DemoClient(new HttpClient { BaseAddress = new Uri("YOUR_API_HOST") });
var pong = await client.Misc.Ping("World");
Console.WriteLine(pong); // Prints "Hello, World" to the output.Built with JetBrains tools for Open Source projects.
MIT © Max Koverdyaev