AutoWasmApiGenerator 2026.5.25.1
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package AutoWasmApiGenerator --version 2026.5.25.1
NuGet\Install-Package AutoWasmApiGenerator -Version 2026.5.25.1
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="AutoWasmApiGenerator" Version="2026.5.25.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoWasmApiGenerator" Version="2026.5.25.1" />
<PackageReference Include="AutoWasmApiGenerator" />
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 AutoWasmApiGenerator --version 2026.5.25.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoWasmApiGenerator, 2026.5.25.1"
#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 AutoWasmApiGenerator@2026.5.25.1
#: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=AutoWasmApiGenerator&version=2026.5.25.1
#tool nuget:?package=AutoWasmApiGenerator&version=2026.5.25.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AutoWasmApiGenerator
介绍
本项目是一个增量生成器,用于生成 BlazorServer 项目的 WebApi 接口,以便在 BlazorAuto 模式下使用。
使用
项目结构
Server端项目Client端项目Shared双端共用的项目,一般是共用的页面、实体模型、等等
使用步骤
- 在 Server 项目中引用 AutoWasmApiGenerator 项目
<PackageReference Include="AutoWasmApiGenerator" Version="0.0.*" /> - 在 Shared 或 Client 项目中添加接口
[WebController] public interface ITestService { [WebMethod(Method = WebMethod.Post)] // 默认指定为Post Task<bool> LogAsync(string message, string path, CancellationToken token); } - 在 Server 项目中任意一个文件标注
WebControllerAssemblyAttribute, 生成的文件为TestServiceController[assembly: AutoWasmApiGenerator.WebControllerAssembly] - 在 Client 项目中或生成控制器调用类的项目中的任意一个文件标注
ApiInvokerAssemblyAttribute, 生成的文件为TestServiceApiInvoker[assembly: AutoWasmApiGenerator.ApiInvokerAssembly] - 在 Client 项目中注册
builder.Services.AddScoped<ITestService, TestServiceApiInvoker>(); - 通过
ITestService调用服务@code { [Inject] public ITestService TestService { get; set; } private async Task LogAsync() { await TestService.LogAsync("Hello World", "path", CancellationToken.None); } }
相关特性介绍
WebControllerAttribute
标注服务接口,根据接口生成控制器和调用类
WebMethodNotSupportedAttribute
标注方法,表示不生成对应的 WebApi
ApiInvokeNotSupportedAttribute
标注类,表示不生成调用类;标注方法, 表示不生成调用方法(调用该方法,将抛出NotSupportedException)
WebMethodAttribute
标注方法,指定请求方式
[WebMethod(Method = WebMethod.Post)]
Task<bool> LogAsync(string message);
属性
| 名称 | 类型 | 说明 |
|---|---|---|
| Method | WebMethod | 指定请求方法,默认为 Post |
| Route | string? | 指定 Action 路由,null 时为方法名称 |
| AllowAnonymous | bool | 是否支持匿名访问,会覆盖 Authorize 设置 |
| Authorize | bool | 是否需要授权 |
WebMethod
可能的值
- Get
- Post
- Put
- Delete
WebMethodParameterBindingAttribute
标注参数,指定参数绑定方式
[WebMethod(Method = WebMethod.Post)]
Task<bool> Log3Async([WebMethodParameterBinding(BindingType.FromBody)] string message, [WebMethodParameterBinding(BindingType.FromQuery)] string path,[WebMethodParameterBinding(BindingType.Ignore)] CancellationToken token);
属性
| 名称 | 类型 | 说明 |
|---|---|---|
| Type | BindingType | 参数绑定类型 |
BindingType
- Ignore 忽略
- FromQuery 从查询字符串中获取值。
- FromRoute 从路由数据中获取值。
- FromForm 从发布的表单域中获取值。
- FromBody 从请求正文中获取值。
- FromHeader 从 HTTP 标头中获取值。
- FromServices 从服务容器中获取值。
可注入服务
IGeneratedApiInvokeDelegatingHandler
接口调用类的切面入口
- Task BeforeSendAsync(SendContext context);
- Task AfterSendAsync(SendContext context);
- Task OnExceptionAsync(ExceptionContext context);
使用示例
[AutoInject(Group = "WASM", ServiceType = typeof(IGeneratedApiInvokeDelegatingHandler))]
public class GeneratedApiHandler(ILogger<GeneratedApiHandler> logger, IUIService ui) : GeneratedApiInvokeDelegatingHandler
{
public override Task BeforeSendAsync(SendContext context)
{
logger.LogDebug("before request {Message}", context.TargetMethod);
return base.BeforeSendAsync(context);
}
public override Task OnExceptionAsync(ExceptionContext context)
{
logger.LogDebug("请求发生异常: {Message}", context.Exception.Message);
if (context.SendContext.Response?.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
context.Handled = true;
ui.Error("用户过期,请重新登录");
}
return Task.CompletedTask;
}
}
SendContext
| 名称 | 类型 | 说明 |
|---|---|---|
| TargetType | Type | 接口类型 |
| TargetMethod | string | 方法名称 |
| Parameters | object?[]? | 方法参数 |
| ReturnType | Type? | 方法返回值 |
| Request | HttpRequestMessage | HttpClient 的请求上下文 |
| Response | HttpResponseMessage? | HttpClient 的响应上下文 |
ExceptionContext
| 名称 | 类型 | 说明 |
|---|---|---|
| SendContext | SendContext | 请求上下文 |
| Exception | Exception | 异常 |
| Handled | bool | 异常是否已经处理,未处理则抛出异常 |
异常返回值处理
AddAutoWasmErrorResultHandler
// 当接口调用类发生异常时,若异常已经处理,需要返回自定义的失败结果时,可根据返回类型进行自定义
builder.Services.AddAutoWasmErrorResultHandler(config =>
{
config.CreateErrorResult<QueryResult>(context =>
{
return new QueryResult() { IsSuccess = false, Message = context.Exception.Message };
});
});
按约束创建返回值
如果是返回统一的自定义返回值,并且该类型拥有无参构造函数,拥有一个类似success的bool类型的属性和一个类似message或者msg的string类型的属性,将尝试 new 一个示例并为这两个属性赋值
自定义属性查找功能
如果未按约束命名,可通过ApiInvokerAssemblyAttribute上的属性辅助查找
| 名称 | 说明 |
|---|---|
| SuccessFlag | 如果有多个值,用 逗号、空格、分号 隔开 |
| MessageFlag | 如果有多个值,用 逗号、空格、分号 隔开 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 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 is compatible. 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. |
| .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
-
net10.0
-
net6.0
-
net8.0
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 |
|---|---|---|
| 2026.5.25.1 | 100 | 5/25/2026 |
| 2026.5.15.1 | 96 | 5/15/2026 |
| 2026.2.12.1 | 128 | 2/12/2026 |
| 0.1.5 | 120 | 1/20/2026 |
| 0.1.4 | 217 | 9/11/2025 |
| 0.1.3 | 215 | 5/27/2025 |
| 0.1.2 | 198 | 5/22/2025 |
| 0.1.2-pre | 188 | 5/22/2025 |
| 0.1.1 | 218 | 11/14/2024 |
| 0.1.0 | 172 | 11/14/2024 |
| 0.0.9 | 168 | 10/31/2024 |
| 0.0.8 | 170 | 10/31/2024 |
| 0.0.7 | 171 | 10/25/2024 |
| 0.0.6 | 151 | 10/25/2024 |
| 0.0.5 | 186 | 10/9/2024 |
| 0.0.4 | 189 | 9/5/2024 |
| 0.0.3 | 174 | 8/29/2024 |
| 0.0.2 | 215 | 8/15/2024 |
| 0.0.1 | 164 | 7/28/2024 |