Biwen.MinimalApi 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Biwen.MinimalApi --version 1.0.0                
NuGet\Install-Package Biwen.MinimalApi -Version 1.0.0                
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="Biwen.MinimalApi" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Biwen.MinimalApi --version 1.0.0                
#r "nuget: Biwen.MinimalApi, 1.0.0"                
#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.
// Install Biwen.MinimalApi as a Cake Addin
#addin nuget:?package=Biwen.MinimalApi&version=1.0.0

// Install Biwen.MinimalApi as a Cake Tool
#tool nuget:?package=Biwen.MinimalApi&version=1.0.0                

Biwen.MinimalApi

minimal api anywhere,no reflection,no emit,just use roslyn source generator for NativeAOT.

Usage

Install Package

dotnet add package Biwen.MinimalApi

Define Api

[MinimalApi("groupName", "/hello", ["GET", "POST"])]
public class HelloApi(RandomService randomService) : IMinimalApi
{
    private readonly RandomService _randomService = randomService;
    public Delegate Handler => [OutputCache] () =>
    {
        return Results.Content($"hello world {_randomService.GetRandom()}");
    };
    public RouteHandlerBuilder HandlerBuilder(RouteHandlerBuilder builder)
    {
        builder.AllowAnonymous();
        return builder;
    }
}

Add Api & Use Api


// 注册 your services
builder.Services.AddOutputCache();
builder.Services.AddSingleton<RandomService>();
// ...
// 添加MinimalApi
builder.Services.AddMinimalApis();

// ...

// 注册MinimalApi路由
app.MapMinimalApis();

// ...

Remark

  • Why use native-aot with aspnet core
  • 引用的项目必须是net8.0+,如果需要NativeAOT编译,需要在项目文件中添加如下配置,推荐直接使用NativeAOT脚手架创建项目
<Project Sdk="Microsoft.NET.Sdk.Web">
	<PropertyGroup>
		<TargetFramework>net8.0</TargetFramework>
		<PublishAot>true</PublishAot>
		<IsAotCompatible>true</IsAotCompatible>
	</PropertyGroup>
</Project>
  • 如果项目需要AOT编译,Json序列化需要使用System.Text.Json(且需要使用System.Text.Json源生成器),Newtonsoft.Json不支持AOT编译

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var todosApi = app.MapGroup("/todos");
todosApi.MapGet("/", () => sampleTodos);
todosApi.MapGet("/{id}", (int id) =>
    sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
        ? Results.Ok(todo)
        : Results.NotFound());

app.Run();

[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}

Report Diagnostic Code

  • GEN101 : 不能将[MinimalApi]标记到抽象类
  • GEN102 : [MinimalApi]标记的类必须实现接口IMinimalApi

Q&A

  • 当前是NativeAOT版本,由于AOT存在诸多限制,所以很多Biwen.QuickApi中的特性不受支持,后续会提供更多的特性支持. 参考:AOT兼容性
  • 为什么要AOT编译? 有什么好处? 最大程度减少磁盘占用空间:使用本机 AOT 发布时,将生成一个可执行文件,其中仅包含支持程序所需的外部依赖项的代码。 减小的可执行文件大小可能会导致: 较小的容器映像,例如在容器化部署方案中。 缩短了较小映像的部署时间。 缩短启动时间:本机 AOT 应用程序可缩短启动时间,这意味着 应用已准备好更快地为请求提供服务。 改进了容器业务流程协调程序需要管理从应用的一个版本到另一个版本的转换的部署。 减少内存需求:本机 AOT 应用可能会减少内存需求,具体由应用执行的工作决定。 减少内存消耗可以提高部署密度和可伸缩性。

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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.

Version Downloads Last updated
1.1.0.1 240 11/20/2023
1.0.0 109 11/18/2023

NET8 NativeAOT support