Afic.Minimal 2026.10.41

dotnet add package Afic.Minimal --version 2026.10.41
                    
NuGet\Install-Package Afic.Minimal -Version 2026.10.41
                    
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="Afic.Minimal" Version="2026.10.41" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Afic.Minimal" Version="2026.10.41" />
                    
Directory.Packages.props
<PackageReference Include="Afic.Minimal" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Afic.Minimal --version 2026.10.41
                    
#r "nuget: Afic.Minimal, 2026.10.41"
                    
#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.
#:package Afic.Minimal@2026.10.41
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Afic.Minimal&version=2026.10.41
                    
Install as a Cake Addin
#tool nuget:?package=Afic.Minimal&version=2026.10.41
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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