Afic.Minimal
2026.10.41
dotnet add package Afic.Minimal --version 2026.10.41
NuGet\Install-Package Afic.Minimal -Version 2026.10.41
<PackageReference Include="Afic.Minimal" Version="2026.10.41" />
<PackageVersion Include="Afic.Minimal" Version="2026.10.41" />
<PackageReference Include="Afic.Minimal" />
paket add Afic.Minimal --version 2026.10.41
#r "nuget: Afic.Minimal, 2026.10.41"
#:package Afic.Minimal@2026.10.41
#addin nuget:?package=Afic.Minimal&version=2026.10.41
#tool nuget:?package=Afic.Minimal&version=2026.10.41
Afic.Minimal
Afic.Net 的 Minimal API 组件:以 [AppModule] / [AppGroup] / [AppService] 三级特性描述应用结构,
由源生成器在编译期产出路由注册代码(IAppEndpoint 实现),运行期不做反射式路由发现,面向 AOT/裁剪场景。
特性
- 编译期生成路由:模块、分组、应用服务的
Map实现与静态句柄Endpoint均由Afic.Minimal.Generator生成,业务类只写业务方法。 - 模板逐级拼接:模块 → 分组 → 应用服务 → 函数的模板层层下传,自动构成最终路由。
- 端点实例按请求解析:应用服务不经启动期注册表,由
IAppEndpointContext.GetInstance<TService>()在请求时按类型解析。 - 统一结果加工:所有处理器经
AppEndpointUtils交IActionResultProvider加工成功/异常结果,并按请求填充ActionExecutingContext。 - 审计级别内联:方法级
[Auditing]在编译期烘焙进生成的处理器,运行期无需再读特性。
引用
<ItemGroup>
<ProjectReference Include="..\..\Afic.Minimal.Generator\Afic.Minimal.Generator.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\Afic.Minimal\Afic.Minimal.csproj" />
</ItemGroup>
消费 NuGet 包时,第二条换成对 Afic.Minimal 的 PackageReference,生成器同样以 Analyzer 方式引入。
快速开始
三类声明各司其职,标注这些特性的类都必须声明为 partial(否则报 AFICMIN001,且句柄无法生成):
// 模块:整个应用的入口,rule 为“命名空间.类名”通配规则,template 为路由前缀
[AppModule("MyApp.**", "/api")]
public sealed partial class AppModule;
// 分组:按“命名空间”归属,逐级子命名空间天然构成父子层级
[AppGroup("MyApp.Features.Todo", "/todo")]
public sealed partial class TodoGroup;
// 应用服务:具体业务类,方法上的 HTTP 方法特性即端点
[AppService]
public sealed partial class TodoApp(TodoService todoService)
{
[HttpGet]
[Auditing(AuditingLevelEnum.FULL)]
public Task<Todo> GetTodo() => todoService.GetAsync();
[HttpPost("/create")]
public Todo Create(Todo todo) => todoService.Create(todo);
}
启动时挂载模块句柄:
var builder = WebApplication.CreateSlimBuilder(args);
var app = builder.Build();
var context = new AppEndpointContext(app); // 自定义的 IAppEndpointContext 实现
context.MapAppModule(AppModule.Endpoint); // 亦可 MapAppGroup / MapAppService
app.Run();
生成的端点类形如 g__TodoAppEndpoint(g__ + 原类名 + Endpoint),恒为 public sealed;
用户类只增加一个静态句柄 public static IAppServiceEndpoint Endpoint { get; }。
路由规则
类级模板回退(特性未写模板时):
| 特性 | 回退结果 |
|---|---|
[AppModule] |
/{kebab-case(类名去掉 Module)} |
[AppGroup] |
/{kebab-case(类名去掉 Group)} |
[AppService] |
/{类名去掉结尾 AppService 或 App},保留原始大小写 |
即 TodoApp → /Todo、UserAppService → /User、AppUser → /AppUser(只剥离结尾后缀,不处理中间的 App)。
方法级回退:HTTP 方法特性未写路径时回退为 /{方法名},同样保留原始大小写(GetTodo → /GetTodo)。
显式空串 [HttpGet("")] 视为“未设置”一并回退,因此没有把单个函数映射到服务组根路径的写法。
模块匹配规则:rule 匹配的是“命名空间.类名”,* 匹配单个命名空间段,** 跨段匹配,规则为空则全部命中。
分组归属规则(精准命名空间匹配,不支持通配):
- 直属应用服务:其命名空间精确等于分组声明的
ns; - 直属子分组:其命名空间恰为分组
ns的直接下一级; - 模块挂载“顶层有效分组”(上一级命名空间未被任何分组声明)与“游离应用服务”(命名空间未被任何分组声明);
- 只有存在归属子节点(直属服务或有效子分组)的分组才会生成,模块与父分组也只引用会被生成的分组。
端点上下文
IAppEndpointContext 是生成代码与宿主之间的唯一契约,由消费方实现:
| 成员 | 说明 |
|---|---|
EndpointRouteBuilder |
当前层级的端点路由构建器 |
CreateContext(IEndpointRouteBuilder) |
围绕给定构建器新建上下文,供下一层级使用 |
GetInstance(Type) |
按类型解析应用服务实例(通常转发到 IoC 容器) |
GetActionResultProvider() |
取结果加工提供程序 |
GetActionFilterProviders() |
取行为过滤提供程序集合(生成代码经 AppEndpointUtils 消费) |
RouteRoot |
路由根地址(生成器未消费,由宿主自行拼接或留空) |
生成代码逐级调用 CreateContext 下传前缀:模块 MapGroup(模块模板) → 分组 MapGroup(分组模板) → 服务 MapGroup(服务模板),
因此实现里只需按传入的构建器重新构造上下文即可。
参数绑定
复杂对象参数(非简单类型、集合、框架特殊类型,且未显式标注绑定特性)由源生成按路由的 HTTP 方法自动补特性:
MapGet/MapDelete → [AsParameters](查询串按属性绑定;Minimal API 的 [FromQuery] 不支持复杂对象,运行期会报错),
MapPost/MapPut → [FromBody],其余方法保持默认推断。查询绑定时不可空属性必须随请求提供,否则返回 400。
结果加工
每个生成的处理器都是一次 AppEndpointUtils.Execute / ExecuteAsync(context, ActionExecutingContext, 级别, () => 业务调用):
ActionExecutingContext由处理器按请求填充:HttpContext(处理器参数末尾追加)、RouteData(httpContext.GetRouteData())、ActionArguments(业务参数名 → 值)、ActionDescriptor(注册期构造一次:DisplayName、RouteValues、Parameters、MetaDatas——应用服务类级与方法级的全部特性实例,类级在前、方法级在后,无法在编译期重建的特性跳过并报AFICMIN004告警);- 同一批特性实例还经
group.Map*之后的.WithMetadata(...)挂到 ASP.NET Core 端点元数据 (HttpContext.GetEndpoint()?.Metadata),顺序与MetaDatas一致,两者共享注册期构造的实例; - 执行器在业务调用前把当前
HttpContext写入容器中的IHttpContextAccessor(经GetInstance(typeof(IHttpContextAccessor))取得), 执行结束后(含异常)恢复原值,使应用服务构造注入的访问器能读到本次请求;宿主未注册该访问器时跳过绑定,行为不变; - 有提供程序:返回值交
OnSuccess(result, level),异常交OnError(ex, level); - 提供程序为
null:成功结果透传为Results.Ok(result),异常原样抛出以保留原始堆栈; - 提供程序返回
null:回退为Results.Empty; - 行为过滤提供程序(
IActionFilterProvider)在业务调用前后分别收到OnActionExecuting与OnActionExecuted。
返回类型按 Task / Task<T> / ValueTask / ValueTask<T> / 非可等待分类,可等待类型生成 async 处理器并 await 后把解包值交给 OnSuccess(无返回值的 Task/ValueTask 落为 null)。
审计
方法级 [Auditing(AuditingLevelEnum.X)],级别在编译期写入生成的处理器;未标注时缺省 NORMAL。
| 级别 | 含义 |
|---|---|
NO |
不审计 |
SIMPLIFY |
只记录响应时间 |
NORMAL |
标准审计,记录入参 |
FULL |
全量审计,记录出参 |
AuditingAttribute 的 AttributeUsage 为 AttributeTargets.Method,不支持类级回退。
诊断
| 编号 | 严重级别 | 说明 |
|---|---|---|
AFICMIN001 |
Error | 标注 [AppModule]/[AppGroup]/[AppService] 的类未声明为 partial |
AFICMIN002 |
Error | 同一 ns 被多个 [AppGroup] 声明,整批分组中止生成 |
AFICMIN003 |
Error | [AppGroup] 的 ns 含通配符(分组归属仅支持精准命名空间) |
AFICMIN004 |
Warning | 特性无法在编译期重建,已跳过写入行为元数据 |
示例
Demo/Afic.Minimal.Demo 覆盖了多层分组、游离应用服务、异步/同步返回、异常分支与审计级别,
其最终路由为:
| 路由 | 归属 |
|---|---|
GET /example/Ping/GetPing |
模块直挂游离服务 |
GET /example/Health/GetHealth |
模块直挂游离服务 |
GET /example/simple/Todo/GetTodo |
模块 → 一级分组 → 服务(FULL 审计) |
GET /example/simple/admin/User/GetUser |
模块 → 一级分组 → 二级分组 → 服务 |
注意事项
- 包当前 TargetFramework 为
net8.0,示例工程为net10.0并使用CreateSlimBuilder;两种宿主皆可挂载生成的路由。 Afic.Minimal内IActionFilterProvider的命名空间为Afic.WebHost.ResultWrapper.ActionFilter(历史遗留), 实现该接口时无需引用Afic.WebHost。- 端点实例按请求解析一次,
GetInstance<TService>()返回null时生成的Resolve()会抛出带类名的InvalidOperationException,便于定位未注册的应用服务。 - 建议在启动处显式挂载模块句柄;
Afic.MinimalHost提供的MapAppModules走反射扫描 +Activator.CreateInstance, 与 AOT 目标相悖,仅适用于非 AOT 宿主。 Afic.MinimalHost仍处于开发中;如需 IoC 版上下文可直接参考其IocAppEndpointContext。
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Afic.Minimal:
| Package | Downloads |
|---|---|
|
Afic.MinimalHost
Minimal API host components for Afic.Net. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.10.41 | 124 | 10/31/2026 |
| 2026.10.40 | 112 | 10/30/2026 |
| 2026.10.38 | 113 | 10/28/2026 |
| 2026.10.37 | 109 | 10/27/2026 |
| 2026.10.36 | 105 | 10/26/2026 |
| 2026.10.35 | 106 | 10/26/2026 |
| 2026.10.34 | 108 | 10/26/2026 |
| 2026.10.33 | 109 | 10/26/2026 |
| 2026.10.32 | 126 | 10/16/2026 |