Biwen.MinimalApi.Gen
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Biwen.MinimalApi.Gen --version 1.0.0
NuGet\Install-Package Biwen.MinimalApi.Gen -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.Gen" 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.Gen --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Biwen.MinimalApi.Gen, 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.Gen as a Cake Addin #addin nuget:?package=Biwen.MinimalApi.Gen&version=1.0.0 // Install Biwen.MinimalApi.Gen as a Cake Tool #tool nuget:?package=Biwen.MinimalApi.Gen&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
- 引用的项目必须是net8.0+,如果需要AOT编译,需要在项目文件中添加如下配置
- Why use
native-aot
with aspnet core
<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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Biwen.MinimalApi.Gen:
Package | Downloads |
---|---|
Biwen.MinimalApi
Biwen.MinimalApi ,NET8+ AOT MinimalApi CQRS |
GitHub repositories
This package is not used by any popular GitHub repositories.
Gen MinimalApis