Skip to content

Commit d28d2ec

Browse files
authored
Merge pull request #158 from dotnet-campus/t/lindexi/AOT
直接路由的方式支持 AOT 发布
2 parents 663b903 + 62e350a commit d28d2ec

23 files changed

+493
-54
lines changed

.github/workflows/dotnet-core.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
3.1.x
1717
6.0.x
1818
8.0.x
19+
9.0.x
1920
2021
- name: Build
2122
run: dotnet build --configuration Release

.github/workflows/dotnet-format.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
3.1.x
2323
6.0.x
2424
8.0.x
25+
9.0.x
2526
2627
- name: Install dotnetCampus.EncodingNormalior
2728
run: dotnet tool update -g dotnetCampus.EncodingNormalior

.github/workflows/nuget-tag-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
3.1.x
2121
6.0.x
2222
8.0.x
23+
9.0.x
2324
2425
- name: Install dotnet tool
2526
run: dotnet tool install -g dotnetCampus.TagToVersion
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IpcDirectRoutedAotDemo;
2+
3+
public record DemoRequest
4+
{
5+
public required string Value { get; init; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IpcDirectRoutedAotDemo;
2+
3+
public record DemoResponse
4+
{
5+
public required string Result { get; init; }
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<PublishAot>true</PublishAot>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\dotnetCampus.Ipc\dotnetCampus.Ipc.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IpcDirectRoutedAotDemo;
2+
3+
public record NotifyInfo
4+
{
5+
public required string Value { get; init; }
6+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using System.Diagnostics;
4+
5+
using dotnetCampus.Ipc.Context;
6+
using dotnetCampus.Ipc.IpcRouteds.DirectRouteds;
7+
using dotnetCampus.Ipc.Pipes;
8+
using IpcDirectRoutedAotDemo;
9+
10+
var notifyPath = "NotifyFoo";
11+
var notifyPath2 = "NotifyFoo2";
12+
var requestPath = "RequestFoo";
13+
var requestPath2 = "RequestFoo2";
14+
15+
if (args.Length == 0)
16+
{
17+
// 首个启动的,当成服务端
18+
string pipeName = Guid.NewGuid().ToString();
19+
var ipcProvider = new IpcProvider(pipeName, new IpcConfiguration()
20+
{
21+
22+
}.UseSystemJsonIpcObjectSerializer(SourceGenerationContext.Default));
23+
var ipcDirectRoutedProvider = new JsonIpcDirectRoutedProvider(ipcProvider);
24+
ipcDirectRoutedProvider.AddNotifyHandler(notifyPath, () =>
25+
{
26+
Console.WriteLine($"[{Environment.ProcessId}] Receive Notify. 服务端收到通知");
27+
});
28+
29+
ipcDirectRoutedProvider.AddNotifyHandler(notifyPath2, (NotifyInfo notifyInfo) =>
30+
{
31+
Console.WriteLine($"[{Environment.ProcessId}] Receive Notify. 服务端收到通知 NotifyInfo={notifyInfo}");
32+
});
33+
34+
ipcDirectRoutedProvider.AddRequestHandler(requestPath, () => "ResponseFoo");
35+
36+
ipcDirectRoutedProvider.AddRequestHandler(requestPath2, (DemoRequest request) =>
37+
{
38+
Console.WriteLine($"[{Environment.ProcessId}] Receive Request. 服务端收到请求 DemoRequest={request}");
39+
return new DemoResponse() { Result = "返回内容" };
40+
});
41+
42+
ipcDirectRoutedProvider.StartServer();
43+
Console.WriteLine($"[{Environment.ProcessId}] 服务启动");
44+
45+
// 启动另一个进程
46+
Process.Start(Environment.ProcessPath!, pipeName);
47+
}
48+
else
49+
{
50+
var peerName = args[0];
51+
Console.WriteLine($"[{Environment.ProcessId}] 客户端进程启动");
52+
var jsonIpcDirectRoutedProvider = new JsonIpcDirectRoutedProvider(ipcConfiguration:new IpcConfiguration()
53+
.UseSystemJsonIpcObjectSerializer(SourceGenerationContext.Default));
54+
JsonIpcDirectRoutedClientProxy jsonIpcDirectRoutedClientProxy = await jsonIpcDirectRoutedProvider.GetAndConnectClientAsync(peerName);
55+
56+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送通知");
57+
await jsonIpcDirectRoutedClientProxy.NotifyAsync(notifyPath);
58+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送通知完成");
59+
60+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送通知2");
61+
await jsonIpcDirectRoutedClientProxy.NotifyAsync(notifyPath2, new NotifyInfo()
62+
{
63+
Value = "通知2"
64+
});
65+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送通知2完成");
66+
67+
68+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送请求");
69+
var responseValue = await jsonIpcDirectRoutedClientProxy.GetResponseAsync<string>(requestPath);
70+
Console.WriteLine($"[{Environment.ProcessId}] 客户端完成请求,收到响应内容 {responseValue}");
71+
72+
Console.WriteLine($"[{Environment.ProcessId}] 客户端发送请求2");
73+
var responseValue2 = await jsonIpcDirectRoutedClientProxy.GetResponseAsync<DemoResponse>(requestPath2, new DemoRequest()
74+
{
75+
Value = "客户端请求内容"
76+
});
77+
Console.WriteLine($"[{Environment.ProcessId}] 客户端完成请求2,收到响应内容 {responseValue2}");
78+
}
79+
80+
Console.WriteLine($"[{Environment.ProcessId}] 等待退出");
81+
Console.Read();
82+
Console.WriteLine($"[{Environment.ProcessId}] 进程准备退出");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json.Serialization;
6+
using System.Threading.Tasks;
7+
8+
namespace IpcDirectRoutedAotDemo;
9+
10+
[JsonSerializable(typeof(NotifyInfo))]
11+
[JsonSerializable(typeof(DemoRequest))]
12+
[JsonSerializable(typeof(DemoResponse))]
13+
internal partial class SourceGenerationContext : JsonSerializerContext
14+
{
15+
}

demo/dotnetCampus.Ipc.Demo/dotnetCampus.Ipc.Demo.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
9+
<PublishAot>true</PublishAot>
910
</PropertyGroup>
1011

1112
<ItemGroup>

0 commit comments

Comments
 (0)