mirror of
https://github.com/RRQM/TouchSocket.git
synced 2025-12-20 10:26:43 +08:00
重构(项目): 支持AOT并优化WebApi配置
重构项目以支持AOT编译,更新目标框架至net10.0,调整项目文件配置。 重构`Program.cs`,改用`HttpService`并优化插件配置,新增JSON序列化上下文以支持AOT。 移除多余的开发环境配置文件,新增自定义序列化器`MySerializerFormatter`。 文档部分整合AOT支持内容至`webapi-serialization.mdx`,更新侧边栏结构。
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<PublishAot>true</PublishAot>
|
||||
<UserSecretsId>dotnet-WebApiConsoleApp-9b0df4ac-2e15-40a3-90cf-2b2d890fe9a5</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
<!--#region WebApiAOT项目配置-->
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<PublishAot>true</PublishAot>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" />
|
||||
<PackageReference Include="TouchSocket.Hosting" />
|
||||
<PackageReference Include="TouchSocket.WebApi" />
|
||||
<PackageReference Include="TouchSocket.WebApi.Swagger" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TouchSocket.WebApi" />
|
||||
</ItemGroup>
|
||||
<!--#endregion-->
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="api\css\index.css">
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Http;
|
||||
using TouchSocket.Rpc;
|
||||
@@ -21,72 +22,71 @@ namespace WebApiConsoleApp
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
var builder = Host.CreateApplicationBuilder(args);
|
||||
HttpService service = new();
|
||||
await service.SetupAsync(new TouchSocketConfig()
|
||||
.SetListenIPHosts(7789)
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
//配置容器
|
||||
a.AddRpcStore(store =>
|
||||
{
|
||||
store.RegisterServer<ApiServer>();//注册服务
|
||||
});
|
||||
a.AddConsoleLogger();
|
||||
})
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
//配置插件
|
||||
a.UseTcpSessionCheckClear();
|
||||
|
||||
builder.Services.ConfigureContainer(a =>
|
||||
#region WebApi配置SystemTextJson序列化器
|
||||
a.UseWebApi(options =>
|
||||
{
|
||||
options.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Clear();
|
||||
converter.AddSystemTextJsonSerializerFormatter(options =>
|
||||
{
|
||||
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||
});
|
||||
});
|
||||
});
|
||||
#endregion
|
||||
|
||||
//配置静态页面服务
|
||||
a.UseHttpStaticPage(options =>
|
||||
{
|
||||
options.AddFolder("api/");
|
||||
});
|
||||
|
||||
//此插件是http的兜底插件,应该最后添加。作用是当所有路由不匹配时返回404.且内部也会处理Option请求。可以更好的处理来自浏览器的跨域探测。
|
||||
a.UseDefaultHttpServicePlugin();
|
||||
}));
|
||||
|
||||
await service.StartAsync();
|
||||
service.Logger.Info("WebApiAOT服务已启动,监听端口:7789");
|
||||
service.Logger.Info("示例地址:http://127.0.0.1:7789/index.html");
|
||||
|
||||
while (true)
|
||||
{
|
||||
a.AddRpcStore(store =>
|
||||
{
|
||||
store.RegisterServer<ApiServer>();//注册服务
|
||||
});
|
||||
|
||||
a.AddAspNetCoreLogger();
|
||||
});
|
||||
|
||||
builder.Services.AddServiceHostedService<IHttpService, HttpService>(config =>
|
||||
{
|
||||
config.SetListenIPHosts(7789)
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseTcpSessionCheckClear();
|
||||
|
||||
a.UseWebApi(options =>
|
||||
{
|
||||
options.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Clear();
|
||||
converter.AddSystemTextJsonSerializerFormatter(options =>
|
||||
{
|
||||
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//a.UseSwagger()
|
||||
//.UseLaunchBrowser();
|
||||
|
||||
//a.UseHttpStaticPage()
|
||||
//.SetNavigateAction(request =>
|
||||
//{
|
||||
// //此处可以设置重定向
|
||||
// return request.RelativeURL;
|
||||
//})
|
||||
//.SetResponseAction(response =>
|
||||
//{
|
||||
// //可以设置响应头
|
||||
//})
|
||||
//.AddFolder("api/");//添加静态页面文件夹,可使用 http://127.0.0.1:7789/index.html 访问静态网页
|
||||
|
||||
////此插件是http的兜底插件,应该最后添加。作用是当所有路由不匹配时返回404.且内部也会处理Option请求。可以更好的处理来自浏览器的跨域探测。
|
||||
//a.UseDefaultHttpServicePlugin();
|
||||
});
|
||||
});
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region WebApiAOT序列化上下文
|
||||
[JsonSerializable(typeof(MyClass))]
|
||||
[JsonSerializable(typeof(MySum))]
|
||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region WebApiAOT服务注册
|
||||
public partial class ApiServer : SingletonRpcServer
|
||||
{
|
||||
private readonly ILog m_logger;
|
||||
@@ -124,4 +124,5 @@ namespace WebApiConsoleApp
|
||||
{
|
||||
public int Sum { get; set; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"WebApiConsoleApp": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ using TouchSocket.Http;
|
||||
|
||||
namespace WebApiServerApp;
|
||||
|
||||
#region WebApi自定义序列化器
|
||||
internal class MySerializerFormatter : ISerializerFormatter<string, HttpContext>
|
||||
{
|
||||
public int Order { get; set; }
|
||||
@@ -32,3 +33,4 @@ internal class MySerializerFormatter : ISerializerFormatter<string, HttpContext>
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -74,17 +74,19 @@ internal class Program
|
||||
{
|
||||
options.ConfigureConverter(converter =>
|
||||
{
|
||||
#region WebApi配置序列化器
|
||||
//配置转换器
|
||||
|
||||
//converter.Clear();//可以选择性的清空现有所有格式化器
|
||||
|
||||
//添加Json格式化器,可以自定义Json的一些设置
|
||||
//添加Json格式化器,可以自定义Json的一些设置
|
||||
converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
|
||||
|
||||
//添加Xml格式化器
|
||||
//converter.AddXmlSerializerFormatter();
|
||||
|
||||
//converter.Add(new MySerializerFormatter());
|
||||
#endregion
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,201 +0,0 @@
|
||||
---
|
||||
id: webapi-aot
|
||||
title: WebAPI AOT支持
|
||||
---
|
||||
|
||||
## 一、AOT 支持
|
||||
|
||||
TouchSocket.WebApi 完整支持 Native AOT 编译。
|
||||
|
||||
### 1.1 基础配置
|
||||
|
||||
在项目文件中启用 AOT:
|
||||
|
||||
```xml showLineNumbers
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<PublishAot>true</PublishAot>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TouchSocket" Version="*" />
|
||||
<PackageReference Include="TouchSocket.WebApi" Version="*" />
|
||||
<PackageReference Include="TouchSocket.Hosting" Version="*" />
|
||||
<PackageReference Include="TouchSocket.Hosting.SourceGenerator" Version="*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
```
|
||||
|
||||
### 1.2 JSON 序列化上下文
|
||||
|
||||
AOT 需要定义 JSON 序列化上下文:
|
||||
|
||||
```csharp showLineNumbers
|
||||
[JsonSerializable(typeof(int))]
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(UserDto))]
|
||||
[JsonSerializable(typeof(List<UserDto>))]
|
||||
public partial class AppJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
public class UserDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
### 1.3 配置序列化器
|
||||
|
||||
使用 System.Text.Json 并指定序列化上下文:
|
||||
|
||||
```csharp showLineNumbers
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebApi()
|
||||
.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Clear();
|
||||
|
||||
// 使用 AOT 友好的序列化器
|
||||
converter.AddSystemTextJsonSerializerFormatter(options =>
|
||||
{
|
||||
options.TypeInfoResolver = AppJsonSerializerContext.Default;
|
||||
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
});
|
||||
});
|
||||
})
|
||||
```
|
||||
|
||||
### 1.4 服务注册
|
||||
|
||||
使用源生成器注册服务:
|
||||
|
||||
```csharp showLineNumbers
|
||||
[GenerateRegister]
|
||||
public partial class ApiServer : SingletonRpcServer
|
||||
{
|
||||
[WebApi(Method = HttpMethodType.Get)]
|
||||
public int Sum(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
[WebApi(Method = HttpMethodType.Get)]
|
||||
public List<UserDto> GetUsers()
|
||||
{
|
||||
return new List<UserDto>
|
||||
{
|
||||
new UserDto { Id = 1, Name = "Alice", Age = 25 },
|
||||
new UserDto { Id = 2, Name = "Bob", Age = 30 }
|
||||
};
|
||||
}
|
||||
|
||||
[WebApi(Method = HttpMethodType.Post)]
|
||||
public string CreateUser([FromBody] UserDto user)
|
||||
{
|
||||
return $"Created: {user.Name}";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 二、完整示例
|
||||
|
||||
### 2.1 Program.cs
|
||||
|
||||
```csharp showLineNumbers
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Http;
|
||||
using TouchSocket.Sockets;
|
||||
using TouchSocket.WebApi;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 配置 TouchSocket 服务
|
||||
builder.Services.AddServiceHostedService<IHttpService, HttpService>(config =>
|
||||
{
|
||||
config.SetListenIPHosts(7789);
|
||||
config.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebApi()
|
||||
.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Clear();
|
||||
converter.AddSystemTextJsonSerializerFormatter(options =>
|
||||
{
|
||||
options.TypeInfoResolver = AppJsonSerializerContext.Default;
|
||||
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
});
|
||||
});
|
||||
|
||||
a.UseDefaultHttpServicePlugin();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
await app.RunAsync();
|
||||
|
||||
// JSON 序列化上下文
|
||||
[JsonSerializable(typeof(int))]
|
||||
[JsonSerializable(typeof(string))]
|
||||
[JsonSerializable(typeof(UserDto))]
|
||||
[JsonSerializable(typeof(List<UserDto>))]
|
||||
public partial class AppJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
// API 服务
|
||||
[GenerateRegister]
|
||||
public partial class ApiServer : SingletonRpcServer
|
||||
{
|
||||
[WebApi(Method = HttpMethodType.Get)]
|
||||
public int Sum(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
[WebApi(Method = HttpMethodType.Get)]
|
||||
public List<UserDto> GetUsers()
|
||||
{
|
||||
return new List<UserDto>
|
||||
{
|
||||
new UserDto { Id = 1, Name = "Alice", Age = 25 },
|
||||
new UserDto { Id = 2, Name = "Bob", Age = 30 }
|
||||
};
|
||||
}
|
||||
|
||||
[WebApi(Method = HttpMethodType.Post)]
|
||||
public string CreateUser([FromBody] UserDto user)
|
||||
{
|
||||
return $"Created: {user.Name}";
|
||||
}
|
||||
}
|
||||
|
||||
public class UserDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2 发布
|
||||
|
||||
```bash
|
||||
dotnet publish -c Release
|
||||
```
|
||||
|
||||
生成的可执行文件体积小、启动快、无需运行时。
|
||||
|
||||
## 三、注意事项
|
||||
|
||||
1. **避免反射**:使用源生成器而不是反射
|
||||
2. **序列化类型**:所有序列化类型必须添加到 `JsonSerializable`
|
||||
3. **第三方库**:确保所有依赖库支持 AOT
|
||||
4. **测试**:AOT 发布前充分测试
|
||||
@@ -3,6 +3,8 @@ id: webapi-serialization
|
||||
title: WebAPI 数据序列化
|
||||
---
|
||||
|
||||
import CustomCodeBlock from './CodeBlocks/CustomCodeBlock';
|
||||
|
||||
## 一、数据序列化
|
||||
|
||||
数据序列化用于将对象转换为传输格式(如 JSON、XML)。TouchSocket.WebApi 支持多种序列化格式。
|
||||
@@ -19,80 +21,85 @@ title: WebAPI 数据序列化
|
||||
|
||||
在添加 WebApi 插件时,可以配置序列化器:
|
||||
|
||||
```csharp showLineNumbers
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebApi()
|
||||
.ConfigureConverter(converter =>
|
||||
{
|
||||
// 可选:清空现有所有格式化器
|
||||
// converter.Clear();
|
||||
|
||||
// 添加 Json 格式化器(Newtonsoft.Json)
|
||||
converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings()
|
||||
{
|
||||
Formatting = Newtonsoft.Json.Formatting.None
|
||||
});
|
||||
|
||||
// 添加 Xml 格式化器
|
||||
converter.AddXmlSerializerFormatter();
|
||||
});
|
||||
})
|
||||
```
|
||||
<CustomCodeBlock region="WebApi配置序列化器"/>
|
||||
|
||||
### 1.3 使用 System.Text.Json
|
||||
|
||||
对于需要 AOT 支持的场景,推荐使用 `System.Text.Json`:
|
||||
|
||||
```csharp showLineNumbers
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebApi()
|
||||
.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Clear();
|
||||
|
||||
converter.AddSystemTextJsonSerializerFormatter(options =>
|
||||
{
|
||||
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
options.WriteIndented = false;
|
||||
});
|
||||
});
|
||||
})
|
||||
```
|
||||
<CustomCodeBlock region="WebApi配置SystemTextJson序列化器"/>
|
||||
|
||||
### 1.4 自定义序列化器
|
||||
|
||||
可以实现自定义的序列化器:
|
||||
|
||||
```csharp showLineNumbers
|
||||
class MySerializerFormatter : ISerializerFormatter<string, HttpContext>
|
||||
{
|
||||
public int Order { get; set; }
|
||||
<CustomCodeBlock region="WebApi自定义序列化器"/>
|
||||
|
||||
public bool TryDeserialize(HttpContext state, in string source, Type targetType, out object target)
|
||||
{
|
||||
// 实现反序列化逻辑
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
## 二、AOT 支持
|
||||
|
||||
public bool TrySerialize(HttpContext state, in object target, out string source)
|
||||
{
|
||||
// 实现序列化逻辑
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
TouchSocket.WebApi 完整支持 Native AOT 编译。
|
||||
|
||||
### 2.1 基础配置
|
||||
|
||||
在项目文件中启用AOT:
|
||||
|
||||
```xml
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
...
|
||||
<PublishAot>true</PublishAot>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <!-- 根据目标平台选择 -->
|
||||
<PublishTrimmed>true</PublishTrimmed>
|
||||
</PropertyGroup>
|
||||
...
|
||||
</Project>
|
||||
```
|
||||
|
||||
添加自定义格式化器:
|
||||
### 2.2 JSON 序列化上下文
|
||||
|
||||
```csharp showLineNumbers
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseWebApi()
|
||||
.ConfigureConverter(converter =>
|
||||
{
|
||||
converter.Add(new MySerializerFormatter());
|
||||
});
|
||||
})
|
||||
AOT 需要定义 JSON 序列化上下文:
|
||||
|
||||
<CustomCodeBlock region="WebApiAOT序列化上下文"/>
|
||||
|
||||
:::tip 提示
|
||||
|
||||
一般情况下,建议为所有需要序列化的类型创建上下文以确保兼容性。包括Rpc方法的`返回类型`、`参数类型`以及类中类等,一切可能被序列化的类型都应包含在上下文中。
|
||||
|
||||
:::
|
||||
|
||||
### 2.3 配置序列化器
|
||||
|
||||
使用 System.Text.Json 并指定序列化上下文:
|
||||
|
||||
<CustomCodeBlock region="WebApi配置SystemTextJson序列化器"/>
|
||||
|
||||
### 2.4 服务注册
|
||||
|
||||
使用源生成器注册服务:
|
||||
|
||||
<CustomCodeBlock region="WebApiAOT服务注册"/>
|
||||
|
||||
### 2.5 发布
|
||||
|
||||
使用以下命令发布 AOT 应用:
|
||||
|
||||
```bash
|
||||
dotnet publish -c Release
|
||||
```
|
||||
|
||||
生成的可执行文件体积小、启动快、无需运行时。
|
||||
|
||||
## 三、注意事项
|
||||
|
||||
### 3.1 序列化注意事项
|
||||
|
||||
1. **格式化器顺序**:可以通过设置 `Order` 属性来控制格式化器的优先级
|
||||
2. **内容协商**:框架会根据请求的 `Accept` 头自动选择合适的格式化器
|
||||
3. **自定义格式化器**:实现 `ISerializerFormatter<string, HttpContext>` 接口即可添加自定义格式化器
|
||||
|
||||
### 3.2 AOT 注意事项
|
||||
|
||||
1. **避免反射**:使用源生成器而不是反射
|
||||
2. **序列化类型**:所有序列化类型必须添加到 `JsonSerializable` 特性
|
||||
3. **第三方库**:确保所有依赖库支持 AOT
|
||||
|
||||
|
||||
@@ -480,7 +480,7 @@ module.exports =
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"label": "16.6 数据序列化",
|
||||
"label": "16.6 数据序列化与AOT",
|
||||
"id": "webapi-serialization"
|
||||
},
|
||||
{
|
||||
@@ -495,12 +495,7 @@ module.exports =
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"label": "16.9 AOT支持",
|
||||
"id": "webapi-aot"
|
||||
},
|
||||
{
|
||||
"type": "doc",
|
||||
"label": "16.10 Swagger文档",
|
||||
"label": "16.9 Swagger文档",
|
||||
"id": "webapi-swagger"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user