A multi process runtime library based on 'SourceGenerator'. 基于 `SourceGenerator` 的多`进程`运行库。
$ dotnet add package JuxtaposeA multi process runtime library based on 'SourceGenerator'.
基于 SourceGenerator 的多进程运行库。
类型、接口、静态类生成代理,无需手动编写RPC相关代码,即可多进程运行;委托和CancellationToken类型的方法参数(其余类型未特殊处理,将会进行序列化,目前回调委托不支持嵌套和CancellationToken);Linux、Windows(其它未测试);Windows&&VisualStudio Only);CancellationToken、委托等在方法完成后会被释放;<ItemGroup>
<PackageReference Include="Juxtapose" Version="1.5.4" />
</ItemGroup>
[Illusion] 特性指定要生成的类型[Illusion(typeof(Greeter), "Juxtapose.Test.GreeterAsIGreeterIllusion")]
public partial class GreeterJuxtaposeContext : JuxtaposeContext
{
}
示例代码将为Greeter生成代理类型Juxtapose.Test.GreeterAsIGreeterIllusion;
Note!!!
JuxtaposeContext;partial关键字;[Illusion] 的多种用法Juxtapose.Test.GreeterIllusion 类型,且不继承接口(静态类型相同用法)[Illusion(typeof(Greeter))]
Juxtapose.Test.GreeterAsIGreeterIllusion 类型且继承IGreeter接口partial 类来使生成的类型派生自目标接口partial class GreeterIllusion : IGreeter
{ }
Juxtapose.Test.HelloGreeter 类型[Illusion(typeof(Greeter), generatedTypeName: "Juxtapose.Test.HelloGreeter")]
Juxtapose.Test.IGreeterIllusionFromIoCContainer 类型(此时Context类需要实现IIoCContainerProvider接口,并提供有效的IServiceProvider)[Illusion(typeof(IGreeterFromServiceProvider), generatedTypeName: "Juxtapose.Test.IGreeterIllusionFromIoCContainer", fromIoCContainer: true)]
在Main方法开始处添加入口点代码,并使用指定上下文
await JuxtaposeEntryPoint.TryAsEndpointAsync(args, GreeterJuxtaposeContext.SharedInstance);
Juxtapose.Test.GreeterAsIGreeterIllusion的对象,并调用其方法,其实际逻辑将在子进程中运行;Windows&&VisualStudio Only)现在会自动附加调试器(启动项目需要直接引用Juxtapose包,以确保依赖包正确引入)
VisualStudio2022 17.0.5 && Win11 21TH2 中进行了测试,理论上是通用)使用默认外部进程激活器时,默认情况下子进程的诊断会被关闭 (dotnet-dump等会无法运行),以避免生成过多诊断支持文件,可以通过如下方式开启子进程诊断:
LocalExternalProcessActivator.EnableDotnetDiagnostics 为 trueSystem.Text.Json 进行消息的序列化与反序列化,需要手动定义 JsonSerializerContext 声明所有类型:[JsonSerializable(typeof(global::Juxtapose.Messages.JuxtaposeAckMessage))]
[JsonSerializable(typeof(global::Juxtapose.Messages.ExceptionMessage))]
[JsonSerializable(typeof(global::Juxtapose.Messages.InstanceMethodInvokeMessage<global::Juxtapose.Messages.ParameterPacks.CancellationTokenSourceCancelParameterPack>))]
[JsonSerializable(typeof(global::Juxtapose.Messages.DisposeObjectInstanceMessage))]
[JsonSerializable(typeof(global::Juxtapose.Messages.CreateObjectInstanceMessage<global::Juxtapose.Messages.ParameterPacks.ServiceProviderGetInstanceParameterPack>))]
// .........
[JsonSourceGenerationOptions(IgnoreReadOnlyProperties = false, IgnoreReadOnlyFields = false, IncludeFields = true, WriteIndented = false)]
partial class SampleJsonSerializerContext : JsonSerializerContext {}
JuxtaposeContext 的 CreateCommunicationMessageCodecFactory 方法
应用前置步骤声明的 JsonSerializerContext partial class SampleJuxtaposeContext : global::Juxtapose.JuxtaposeContext
{
protected override ICommunicationMessageCodecFactory CreateCommunicationMessageCodecFactory()
{
var communicationMessageCodec = new DefaultJsonBasedMessageCodec(GetMessageTypes(), LoggerFactory, SampleJsonSerializerContext.Default.Options);
return new GenericSharedMessageCodecFactory(communicationMessageCodec);
}
}
Context 代码中,可从分析器中找到代码,复制后可直接使用;SourceGenerator在编译时生成代理类型,封装通信消息。在创建代理类型对象时,会自动创建子进程,并在子进程中创建目标类型的对象,使用命名管道进行进程间通信,使用System.Text.Json进行消息的序列化与反序列化。
| 关键字 | 名称 | 来源 | 作用 |
|---|---|---|---|
| Context | 上下文 | 手动定义 | 用于承载所有子进程运行的相关信息 |
| Executor | 执行器 | 自动生成 | 用于运行时消息解析收发,创建对象,执行静态方法等 |
| ParameterPack | 参数包 | 自动生成 | 将方法参数封装到一个类型中,以便序列化 |
| Illusion | 幻象 | 自动生成 | 实际使用的类 |
| RealObjectInvoker | 真实对象执行器 | 自动生成 | 在子进程中接收消息,并进行实际的对象方法调用 |

| 项目 | 内容 |
|---|---|
| SampleLibrary | 基于库的使用示例,可由其它程序直接使用 |
| SampleConsoleApp | 基于控制台的使用示例,可使用当前程序集生成的类,或使用其他库生成的类 |
| ResourceBasedObjectPool | 基于系统资源的动态对象池示例 |