NexArc.InterfaceBridge.CodeGenerator
1.0.2
See the version list below for details.
dotnet add package NexArc.InterfaceBridge.CodeGenerator --version 1.0.2
NuGet\Install-Package NexArc.InterfaceBridge.CodeGenerator -Version 1.0.2
<PackageReference Include="NexArc.InterfaceBridge.CodeGenerator" Version="1.0.2" />
<PackageVersion Include="NexArc.InterfaceBridge.CodeGenerator" Version="1.0.2" />
<PackageReference Include="NexArc.InterfaceBridge.CodeGenerator" />
paket add NexArc.InterfaceBridge.CodeGenerator --version 1.0.2
#r "nuget: NexArc.InterfaceBridge.CodeGenerator, 1.0.2"
#:package NexArc.InterfaceBridge.CodeGenerator@1.0.2
#addin nuget:?package=NexArc.InterfaceBridge.CodeGenerator&version=1.0.2
#tool nuget:?package=NexArc.InterfaceBridge.CodeGenerator&version=1.0.2
NexArc.InterfaceBridge
A type-safe, interface-based REST API framework for .NET that creates tightly coupled connections between client and server with full AOT (Ahead-of-Time) compilation support.
Features
- Type-Safe API Contracts: Define your REST APIs using C# interfaces with attributes
- AOT-Compatible: Full support for iOS and other platforms requiring AOT compilation
- Source Generation: Automatic client code generation using Roslyn source generators
- System.Text.Json Integration: Built-in support for
JsonSerializablepartial classes - Minimal Boilerplate: Write interfaces once, generate both client and server implementations
- Multiple Content Types: Supports form data, multipart/form-data, and file uploads
- Async/Await: Built-in support for
Task<T>andCancellationToken
Installation
Install the NuGet packages:
# For server-side implementation
dotnet add package NexArc.InterfaceBridge
dotnet add package NexArc.InterfaceBridge.Server
# For client-side code generation
dotnet add package NexArc.InterfaceBridge
dotnet add package NexArc.InterfaceBridge.CodeGenerator
Quick Start
1. Define Your API Interface (Shared)
using NexArc.InterfaceBridge;
[RestConnector("api/hello")]
public interface IHelloApi
{
[Rest(HttpMethod.Get, "greet/{name}")]
Task<string> Greet(string name, CancellationToken cancellationToken = default);
}
2. Implement the Server
using NexArc.InterfaceBridge.Server;
var builder = WebApplication.CreateBuilder(args);
// Register the InterfaceBridge
builder.UseInterfaceBridge<IHelloApi, HelloApi>();
var app = builder.Build();
// Map routes based on interface methods
app.UseInterfaceBridges();
app.Run("http://localhost:5199");
public class HelloApi : IHelloApi
{
public Task<string> Greet(string name, CancellationToken cancellationToken = default)
{
return Task.FromResult($"Hello, {name}!");
}
}
3. Generate and Use the Client
using Examples.Client;
using NexArc.InterfaceBridge;
// Create a partial class with the Bridge attribute
[Bridge(typeof(IHelloApi))]
public partial class HelloClient : IHelloApi
{
public HttpClient HttpClient { get; }
public HelloClient(HttpClient httpClient)
{
HttpClient = httpClient;
}
}
// Use the generated client
var http = new HttpClient
{
BaseAddress = new Uri("http://localhost:5199/")
};
var client = new HelloClient(http);
var result = await client.Greet("World");
Console.WriteLine(result); // Output: Hello, World!
AOT Support with System.Text.Json
For iOS and other AOT platforms, use JsonSerializable partial classes:
using System.Text.Json.Serialization;
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(YourResponseType))]
[JsonSerializable(typeof(YourRequestType))]
public partial class AppJsonContext : JsonSerializerContext { }
[Bridge(typeof(IYourApi), JsonSerializerContext = typeof(AppJsonContext))]
public partial class YourClient : IYourApi
{
public HttpClient HttpClient { get; }
public YourClient(HttpClient httpClient)
{
HttpClient = httpClient;
}
}
Attributes
[RestConnector]
Applied to interfaces to define the base route prefix for all methods.
[Rest]
Applied to interface methods to define HTTP method and route template. Supports:
- GET: Parameters from route template, remaining in query string
- POST/PUT/PATCH: Parameters from route template, remaining in body (form-encoded or multipart)
- DELETE: Parameters from route template
[Bridge]
Applied to partial client classes to generate the implementation. Parameters:
bridgeInterface: The interface to implementJsonSerializerContext(optional): For AOT supportDefaultBodyType(optional): Override default body encoding
Content Types
The framework automatically selects the appropriate content type:
- FormUrlEncoded: Default for POST/PUT/PATCH
- MultipartFormData: Automatically used when
FilePartparameters are present - JSON: Use with
JsonSerializerContextfor AOT support
Advanced Features
File Uploads
Use FilePart for file uploads (automatically switches to multipart/form-data):
[Rest(HttpMethod.Post, "upload")]
Task<UploadResponse> UploadFile(string description, FilePart file, CancellationToken cancellationToken = default);
Custom Body Types
Override the default body type:
[Rest(HttpMethod.Post, "data", BodyType = BodyType.MultipartFormData)]
Task PostData(string name, int value, CancellationToken cancellationToken = default);
License
MIT
Repository
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.