Skip to content

Commit 33ac2c8

Browse files
committed
3.88.4240
★★公布、添加功能 添加 Rafy.Grpc,以支持使用 Grpc 来作为底层数据传输信道。
1 parent 9f4f5c2 commit 33ac2c8

File tree

13 files changed

+306
-3
lines changed

13 files changed

+306
-3
lines changed

Rafy.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rafy.UI", "Rafy\Rafy.UI\Raf
9090
EndProject
9191
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RafyUnitTest.ServerPortal", "Test\RafyUnitTest.ServerPortal\RafyUnitTest.ServerPortal.csproj", "{0008CCD4-9313-4202-9DA0-60D86E29A986}"
9292
EndProject
93+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rafy.Grpc", "Rafy\Plugins\Rafy.Grpc\Rafy.Grpc.csproj", "{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}"
94+
EndProject
9395
Global
9496
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9597
D_UIA_Standard|Any CPU = D_UIA_Standard|Any CPU
@@ -338,6 +340,14 @@ Global
338340
{0008CCD4-9313-4202-9DA0-60D86E29A986}.Debug|Any CPU.Build.0 = Debug|Any CPU
339341
{0008CCD4-9313-4202-9DA0-60D86E29A986}.Release|Any CPU.ActiveCfg = Release|Any CPU
340342
{0008CCD4-9313-4202-9DA0-60D86E29A986}.Release|Any CPU.Build.0 = Release|Any CPU
343+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.D_UIA_Standard|Any CPU.ActiveCfg = Debug|Any CPU
344+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.D_UIA_Standard|Any CPU.Build.0 = Debug|Any CPU
345+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.D_Web|Any CPU.ActiveCfg = Debug|Any CPU
346+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.D_Web|Any CPU.Build.0 = Debug|Any CPU
347+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
348+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
349+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
350+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1}.Release|Any CPU.Build.0 = Release|Any CPU
341351
EndGlobalSection
342352
GlobalSection(SolutionProperties) = preSolution
343353
HideSolutionNode = FALSE
@@ -378,6 +388,7 @@ Global
378388
{653E7621-7A27-4DB0-9915-64EBA291A63F} = {1E609049-148F-4954-97CE-810A78A9036D}
379389
{33AEE526-9850-486B-A03C-C3B2A310ABDC} = {96BE2240-F243-41C6-B050-CDD83504633B}
380390
{0008CCD4-9313-4202-9DA0-60D86E29A986} = {EE01A744-1379-4CDE-93EE-90AD8A9B91AC}
391+
{1B55F2EC-7990-4C41-8B1E-65F345CE9AF1} = {DC52A572-512A-4D70-81C0-F0EF8B6FFC85}
381392
EndGlobalSection
382393
GlobalSection(ExtensibilityGlobals) = postSolution
383394
SolutionGuid = {3EE6CCD8-2729-4C3F-9779-1761004827A5}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
package Rafy.Grpc;
3+
service DataTransferService {
4+
rpc Call (GrpcData) returns (GrpcData) {}
5+
}
6+
7+
message GrpcData
8+
{
9+
bytes data = 1;
10+
}
11+
12+
//https://www.cnblogs.com/MicroHeart/p/13824612.html
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************
2+
*
3+
* 作者:胡庆访
4+
* 创建日期:20220226
5+
* 说明:此文件只包含一个类,具体内容见类型注释。
6+
* 运行环境:.NET 4.0
7+
* 版本号:1.0.0
8+
*
9+
* 历史记录:
10+
* 创建文件 胡庆访 20220226 19:29
11+
*
12+
*******************************************************/
13+
14+
15+
using Rafy.DataPortal;
16+
using System;
17+
using System.Runtime.Serialization;
18+
19+
namespace Rafy.Grpc
20+
{
21+
/// <summary>
22+
/// Grpc 调用请求对象。
23+
/// </summary>
24+
[Serializable]
25+
internal class GrpcCallRequest
26+
{
27+
public object Instance;
28+
public string Method;
29+
public object[] Arguments;
30+
public DataPortalContext Context;
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Google.Protobuf;
2+
using Grpc.Core;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Rafy.Grpc
8+
{
9+
internal class GrpcClient
10+
{
11+
/// <summary>
12+
/// Target of the channel
13+
/// </summary>
14+
public string Target { get; set; }
15+
16+
public byte[] CallCore(byte[] data)
17+
{
18+
Channel channel = new Channel(this.Target, ChannelCredentials.Insecure);
19+
20+
var client = new DataTransferService.DataTransferServiceClient(channel);
21+
var reply = client.Call(new GrpcData { Data = ByteString.CopyFrom(data) });
22+
23+
channel.ShutdownAsync();
24+
25+
return reply.Data.ToByteArray();
26+
}
27+
}
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*******************************************************
2+
*
3+
* 作者:胡庆访
4+
* 创建日期:20220226
5+
* 说明:此文件只包含一个类,具体内容见类型注释。
6+
* 运行环境:.NET 4.0
7+
* 版本号:1.0.0
8+
*
9+
* 历史记录:
10+
* 创建文件 胡庆访 20220226 19:23
11+
*
12+
*******************************************************/
13+
14+
using Rafy.DataPortal;
15+
using Rafy.Serialization;
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Text;
19+
20+
namespace Rafy.Grpc
21+
{
22+
/// <summary>
23+
/// 使用 Grpc 客户端来实现的数据门户代理。
24+
/// </summary>
25+
public class GrpcClientProxy : IDataPortalProxy
26+
{
27+
public DataPortalResult Call(object obj, string method, object[] arguments, DataPortalContext context)
28+
{
29+
var request = new GrpcCallRequest
30+
{
31+
Instance = obj,
32+
Method = method,
33+
Arguments = arguments,
34+
Context = context,
35+
};
36+
37+
var requestBytes = BinarySerializer.SerializeBytes(request);
38+
39+
var client = GetClient();
40+
41+
var responseBytes = client.CallCore(requestBytes);
42+
43+
var response = BinarySerializer.DeserializeBytes(responseBytes);
44+
45+
if (response is Exception responseEx)
46+
{
47+
throw new DataPortalException("连接服务器时,发生异常,请检查 InnerException。", responseEx);
48+
}
49+
return (DataPortalResult)response;
50+
}
51+
52+
private GrpcClient GetClient()
53+
{
54+
var client = new GrpcClient();
55+
client.Target = ConfigurationHelper.GetAppSettingOrDefault("Rafy:Grpc:GrpcClientProxy:Taget", "127.0.0.1:9007");
56+
57+
return client;
58+
}
59+
}
60+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<None Remove="DataTransferService.proto" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Google.Protobuf" Version="3.19.4" />
13+
<PackageReference Include="Grpc" Version="2.44.0" />
14+
<PackageReference Include="Grpc.Tools" Version="2.44.0">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\Rafy\Rafy.csproj" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<Protobuf Include="DataTransferService.proto" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Google.Protobuf;
2+
using Grpc.Core;
3+
using Rafy.DataPortal;
4+
using Rafy.Serialization;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Rafy.Grpc
11+
{
12+
/// <summary>
13+
/// Rafy DataPortal 的核心 Grpc 服务。
14+
/// </summary>
15+
public class RafyDataTransferService : DataTransferService.DataTransferServiceBase
16+
{
17+
public override Task<GrpcData> Call(GrpcData GrpcCallRequest, ServerCallContext context)
18+
{
19+
var requestBytes = GrpcCallRequest.Data.ToByteArray();
20+
var request = BinarySerializer.DeserializeBytes(requestBytes) as GrpcCallRequest;
21+
22+
LogCalling(request);
23+
24+
var portal = new FinalDataPortal();
25+
26+
object result;
27+
try
28+
{
29+
result = portal.Call(request.Instance, request.Method, request.Arguments, request.Context);
30+
}
31+
catch (Exception ex)
32+
{
33+
result = ex;
34+
LogException(request, ex);
35+
}
36+
37+
var responseBytes = BinarySerializer.SerializeBytes(result);
38+
39+
if (!(result is Exception))
40+
{
41+
LogCalled(request, responseBytes);
42+
}
43+
44+
return Task.FromResult(new GrpcData
45+
{
46+
Data = ByteString.CopyFrom(responseBytes)
47+
});
48+
}
49+
50+
private void LogCalling(GrpcCallRequest request)
51+
{
52+
var content = $"Grpc Call invoking. request: method:{request.Method}, instance: {request.Instance}, arguments.length:{request.Arguments.Length}";
53+
var totalBytes = GetTotalBytes(request);
54+
if (totalBytes > 0)
55+
{
56+
content += $", arguments bytes:{totalBytes}";
57+
}
58+
Logger.LogInfo(content);
59+
}
60+
61+
private void LogCalled(GrpcCallRequest request, byte[] response)
62+
{
63+
var content = $"Grpc Call invoked. request: method:{request.Method}, instance: {request.Instance}, arguments.length:{request.Arguments.Length}";
64+
if (response is byte[] bytes)
65+
{
66+
content += $", result bytes:{bytes.Length}";
67+
}
68+
Logger.LogInfo(content);
69+
}
70+
71+
private void LogException(GrpcCallRequest request, Exception ex)
72+
{
73+
Logger.LogError($"Grpc Call Exception occurred! request: method:{request.Method}, instance: {request.Instance}, arguments.length:{request.Arguments.Length}.", ex);
74+
}
75+
76+
private int GetTotalBytes(GrpcCallRequest request)
77+
{
78+
var totalBytes = 0;
79+
80+
var arguments = request.Arguments;
81+
for (int i = 0, c = arguments.Length; i < c; i++)
82+
{
83+
var argument = arguments[i];
84+
if (argument is byte[] bytes)
85+
{
86+
totalBytes += bytes.Length;
87+
}
88+
}
89+
90+
return totalBytes;
91+
}
92+
}
93+
}

Rafy/Rafy/Env/DataPortal/Server/FinalDataPortal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Rafy.DataPortal
2424
/// 最终调用实体的 IDataPortalServer 门户实现。
2525
/// 不论是通过 WCFProxy、还是通过 FakeRemoteProxy,都会调用到 FinalDataPortal 中。
2626
/// </summary>
27-
internal class FinalDataPortal : IDataPortalServer
27+
public class FinalDataPortal : IDataPortalServer
2828
{
2929
public DataPortalResult Call(object obj, string method, object[] parameters, DataPortalContext context)
3030
{

Rafy/_Items/Rafy Version Log.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*
1111
*******************************************************/
1212

13+
3.88.4240
14+
★★公布、添加功能
15+
添加 Rafy.Grpc,以支持使用 Grpc 来作为底层数据传输信道。
16+
1317
3.87.4229
1418
★★公布、添加接口
1519
* 支持 Rafy.Domain.ORM.Query.FactoryMethods 简化 SqlTree 的构造。

Test/RafyUnitTest.ClientTest/App.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
</connectionStrings>
4949
<!--如果要测试 WCF 服务,请运行 RafyUnitTest.ServerPortal,并打开以下配置:-->
5050
<!--<rafy dataPortalProxy="Rafy.DataPortal.WCF.ClientProxy, Rafy">-->
51+
<!--如果要测试 Grpc 服务,请运行 RafyUnitTest.ServerPortal,并打开以下配置:-->
52+
<!--<rafy dataPortalProxy="Rafy.Grpc.GrpcClientProxy, Rafy.Grpc">-->
5153
<rafy>
5254
<plugins>
5355
<add plugin="Rafy.UnitTest.DataProvider"/>

0 commit comments

Comments
 (0)