Skip to content

Commit ba8bfe9

Browse files
authored
Optimization (TouchSocket): Upgrade the target framework and dependen… (#10330)
* Optimization (TouchSocket): Upgrade the target framework and dependency versions. Upgrade all projects to .NET 10.0 and update the dependency packages to the latest versions. * 新增(Program): 增加管道配置及优化内存管理 新增对 System.Buffers 和 System.IO.Pipelines 的引用,优化内存分配和数据流处理。在 TouchSocketConfig 中新增 ReceivePipeOptions 和 SendPipeOptions 配置,支持自定义管道选项。将部分变量声明改为显式类型以提升代码可读性。新增对 System.Text.Json.Serialization 和 TouchSocket.Rpc 的引用,为后续功能扩展做准备。容器配置中新增 AddRpcStore 调用,支持 RPC 功能。 * 新增(Program.cs): 配置管道选项优化服务器性能 引入 `System.Buffers` 和 `System.IO.Pipelines` 命名空间以支持内存池和管道功能。将 `server` 变量的声明从 `var` 改为显式类型 `MyServer`。在 `SetupAsync` 方法中,添加 `TransportOption` 配置,设置 `BufferOnDemand` 为 `false`,并配置 `ReceivePipeOptions` 和 `SendPipeOptions`,以优化服务器性能。删除多余空行。
1 parent 732b6bf commit ba8bfe9

File tree

13 files changed

+90
-26
lines changed

13 files changed

+90
-26
lines changed

frameworks/CSharp/touchsocket/src/TouchSocketHttp/Program.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Buffers;
2+
using System.IO.Pipelines;
13
using System.Text;
24
using TouchSocket.Core;
35
using TouchSocket.Http;
@@ -10,14 +12,32 @@ public class Program
1012
private static async Task Main(string[] args)
1113
{
1214
int port = 8080;
13-
var service = new MyHttpService();
15+
MyHttpService service = new MyHttpService();
1416

1517
await service.SetupAsync(new TouchSocketConfig()
1618
.SetListenIPHosts(port)
1719
.SetMaxCount(1000000)
1820
.SetTransportOption(options =>
1921
{
2022
options.BufferOnDemand = false;
23+
24+
options.ReceivePipeOptions = new PipeOptions(
25+
pool: MemoryPool<byte>.Shared,
26+
readerScheduler: PipeScheduler.ThreadPool,
27+
writerScheduler: PipeScheduler.ThreadPool,
28+
pauseWriterThreshold: 1024 * 1024,
29+
resumeWriterThreshold: 1024 * 512,
30+
minimumSegmentSize: -1,
31+
useSynchronizationContext: false);
32+
33+
options.SendPipeOptions = new PipeOptions(
34+
pool: MemoryPool<byte>.Shared,
35+
readerScheduler: PipeScheduler.ThreadPool,
36+
writerScheduler: PipeScheduler.ThreadPool,
37+
pauseWriterThreshold: 64 * 1024,
38+
resumeWriterThreshold: 32 * 1024,
39+
minimumSegmentSize: -1,
40+
useSynchronizationContext: false);
2141
})
2242
.ConfigureContainer(a =>
2343
{
@@ -48,8 +68,8 @@ internal sealed class MyHttpSessionClient : HttpSessionClient
4868

4969
protected override async Task OnReceivedHttpRequest(HttpContext httpContext)
5070
{
51-
var request = httpContext.Request;
52-
var response = httpContext.Response;
71+
HttpRequest request = httpContext.Request;
72+
HttpResponse response = httpContext.Response;
5373

5474
switch (request.RelativeURL)
5575
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<OutputType>Exe</OutputType>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0-rc.46" />
11+
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0" />
1212
</ItemGroup>
1313
</Project>
1414

frameworks/CSharp/touchsocket/src/TouchSocketHttp31/TouchSocketHttp31.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<OutputType>Exe</OutputType>
77
<ImplicitUsings>enable</ImplicitUsings>

frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform/Program.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// 感谢您的下载和使用
1111
// ------------------------------------------------------------------------------
1212

13+
using System.Buffers;
14+
using System.IO.Pipelines;
1315
using TouchSocket.Core;
1416
using TouchSocket.Sockets;
1517

@@ -19,18 +21,40 @@ internal class Program
1921
{
2022
private static async Task Main(string[] args)
2123
{
22-
var server = new MyServer();
24+
MyServer server = new MyServer();
2325
await server.SetupAsync(new TouchSocketConfig()
2426
.SetListenIPHosts(8080)
2527
.SetMaxCount(1000000)
28+
.SetTransportOption(options =>
29+
{
30+
options.BufferOnDemand = false;
31+
32+
options.ReceivePipeOptions = new PipeOptions(
33+
pool: MemoryPool<byte>.Shared,
34+
readerScheduler: PipeScheduler.ThreadPool,
35+
writerScheduler: PipeScheduler.ThreadPool,
36+
pauseWriterThreshold: 1024 * 1024,
37+
resumeWriterThreshold: 1024 * 512,
38+
minimumSegmentSize: -1,
39+
useSynchronizationContext: false);
40+
41+
options.SendPipeOptions = new PipeOptions(
42+
pool: MemoryPool<byte>.Shared,
43+
readerScheduler: PipeScheduler.ThreadPool,
44+
writerScheduler: PipeScheduler.ThreadPool,
45+
pauseWriterThreshold: 64 * 1024,
46+
resumeWriterThreshold: 32 * 1024,
47+
minimumSegmentSize: -1,
48+
useSynchronizationContext: false);
49+
})
2650
.ConfigureContainer(a =>
2751
{
2852
a.AddConsoleLogger();
2953
}));
3054

3155
await server.StartAsync();
3256
Console.WriteLine("HTTP服务器已启动,端口: 8080");
33-
57+
3458
while (true)
3559
{
3660
Console.ReadLine();

frameworks/CSharp/touchsocket/src/TouchSocketHttpPlatform/TouchSocketHttpPlatform.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="TouchSocket" Version="4.0.0-rc.46" />
11+
<PackageReference Include="TouchSocket" Version="4.0.0" />
1212
</ItemGroup>
1313
</Project>

frameworks/CSharp/touchsocket/src/TouchSocketWebApi/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Buffers;
2+
using System.IO.Pipelines;
13
using System.Text;
24
using System.Text.Json.Serialization;
35
using TouchSocket.Core;
@@ -21,6 +23,24 @@ public static void Main(string[] args)
2123
.SetTransportOption(options =>
2224
{
2325
options.BufferOnDemand = false;
26+
27+
options.ReceivePipeOptions = new PipeOptions(
28+
pool: MemoryPool<byte>.Shared,
29+
readerScheduler: PipeScheduler.ThreadPool,
30+
writerScheduler: PipeScheduler.ThreadPool,
31+
pauseWriterThreshold: 1024 * 1024,
32+
resumeWriterThreshold: 1024 * 512,
33+
minimumSegmentSize: -1,
34+
useSynchronizationContext: false);
35+
36+
options.SendPipeOptions = new PipeOptions(
37+
pool: MemoryPool<byte>.Shared,
38+
readerScheduler: PipeScheduler.ThreadPool,
39+
writerScheduler: PipeScheduler.ThreadPool,
40+
pauseWriterThreshold: 64 * 1024,
41+
resumeWriterThreshold: 32 * 1024,
42+
minimumSegmentSize: -1,
43+
useSynchronizationContext: false);
2444
})
2545
.ConfigureContainer(a =>
2646
{
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk.Worker">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<UserSecretsId>dotnet-WorkerService1-19b37b17-6043-4334-ad9a-9e0e3c670da3</UserSecretsId>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
12-
<PackageReference Include="TouchSocket.Hosting" Version="4.0.0-rc.46" />
13-
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0-rc.46" />
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
12+
<PackageReference Include="TouchSocket.Hosting" Version="4.0.0" />
13+
<PackageReference Include="TouchSocket.WebApi" Version="4.0.0" />
1414
</ItemGroup>
1515
</Project>

frameworks/CSharp/touchsocket/src/TouchSocketWebApi31/TouchSocketWebApi31.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Worker">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<InvariantGlobalization>true</InvariantGlobalization>
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
1515
<PackageReference Include="TouchSocket.Hosting" Version="3.1.0" />
1616
<PackageReference Include="TouchSocket.WebApi" Version="3.1.0" />
1717
</ItemGroup>

frameworks/CSharp/touchsocket/touchsocket-http.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
1+
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
22
WORKDIR /app
33
COPY src/TouchSocketHttp .
44
RUN dotnet publish -c Release -o out
55

6-
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime
6+
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
77

88
WORKDIR /app
99
COPY --from=build /app/out ./

frameworks/CSharp/touchsocket/touchsocket-http31.dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
1+
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
22
WORKDIR /app
33
COPY src/TouchSocketHttp31 .
44
RUN dotnet publish -c Release -o out
55

6-
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
6+
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
77

88
WORKDIR /app
99
COPY --from=build /app/out ./

0 commit comments

Comments
 (0)