{"id":18300794,"url":"https://github.com/stratosblue/juxtapose","last_synced_at":"2026-02-06T10:21:13.703Z","repository":{"id":59470202,"uuid":"430669852","full_name":"stratosblue/Juxtapose","owner":"stratosblue","description":"A multi process runtime library based on 'SourceGenerator'. 基于 `SourceGenerator` 的多`进程`运行库。","archived":false,"fork":false,"pushed_at":"2023-11-21T05:50:52.000Z","size":504,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-26T22:42:36.279Z","etag":null,"topics":["aot","dotnet6","external-process","externalprocess","multi-process","multiprocess","outproc","source-generator","sub-process","subprocess"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stratosblue.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-22T10:57:01.000Z","updated_at":"2024-06-01T04:36:51.212Z","dependencies_parsed_at":"2024-06-01T04:36:48.154Z","dependency_job_id":"11be95b8-73b1-45f3-8a2a-aa129910793d","html_url":"https://github.com/stratosblue/Juxtapose","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stratosblue%2FJuxtapose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stratosblue%2FJuxtapose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stratosblue%2FJuxtapose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stratosblue%2FJuxtapose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stratosblue","download_url":"https://codeload.github.com/stratosblue/Juxtapose/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247352239,"owners_count":20925234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aot","dotnet6","external-process","externalprocess","multi-process","multiprocess","outproc","source-generator","sub-process","subprocess"],"created_at":"2024-11-05T15:13:23.407Z","updated_at":"2026-02-06T10:21:13.651Z","avatar_url":"https://github.com/stratosblue.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Juxtapose\n## 1. Intro\nA multi process runtime library based on 'SourceGenerator'.\n基于 `SourceGenerator` 的多`进程`运行库。\n\n## 2. Features\n - 可以为`类型`、`接口`、`静态类`生成代理，无需手动编写RPC相关代码，即可`多进程`运行；\n - 编译时生成所有代码，运行时无显式的反射调用和动态构造；\n - 支持`委托`和`CancellationToken`类型的方法参数（其余类型未特殊处理，将会进行序列化，目前回调`委托`不支持嵌套和`CancellationToken`）；\n - 支持`Linux`、`Windows`（其它未测试）；\n - 支持调试子进程（`Windows`\u0026\u0026`VisualStudio` Only）；\n - AOT支持*（net8.0+）；\t\t\t\t\t\t\t\t\t\t\n\n### 注意事项\n - 目前参数不支持定义为父类型，实际传递子类型，序列化时将会按照定义的类型进行序列化和反序列化，会导致具体类型丢失；\n - 目前所有的参数都不应该在方法完成后进行保留，`CancellationToken`、`委托`等在方法完成后会被释放；\n\n## 3. Requirement\n - .Net6.0+(其它版本没有尝试过)\n\n## 4. 使用方法\n\n### 4.1 引用包\n```XML\n\u003cItemGroup\u003e\n  \u003cPackageReference Include=\"Juxtapose\" Version=\"1.4.0\" /\u003e\n  \u003c!--\u003cPackageReference Include=\"Juxtapose.SourceGenerator\" Version=\"1.0.0\" /\u003e 1.0.2 以后不再需要单独引用 SourceGenerator 包--\u003e\n\u003c/ItemGroup\u003e\n```\n\n------\n\n### 4.2 建立上下文\n\n#### 4.2.1 创建上下文类型，并使用 `[Illusion]` 特性指定要生成的类型\n\n```C#\n[Illusion(typeof(Greeter), \"Juxtapose.Test.GreeterAsIGreeterIllusion\")]\npublic partial class GreeterJuxtaposeContext : JuxtaposeContext\n{\n}\n```\n示例代码将为`Greeter`生成代理类型`Juxtapose.Test.GreeterAsIGreeterIllusion`；\n  \nNote!!!\n - 必须继承`JuxtaposeContext`；\n - 必须标记`partial`关键字；\n\n------\n\n#### 4.2.2 `[Illusion]` 的多种用法\n\n - 直接为类型生成代理，如下示例生成 `Juxtapose.Test.GreeterIllusion` 类型，且不继承接口（静态类型相同用法）\n  ```C#\n  [Illusion(typeof(Greeter))]\n  ```\n\n - ~~为类型生成代理，并继承指定接口，如下示例生成 `Juxtapose.Test.GreeterAsIGreeterIllusion` 类型且继承`IGreeter`接口~~\n\t- 现在需要手动声明 `partial` 类来使生成的类型派生自目标接口\n  ```C#\n  partial class GreeterIllusion : IGreeter\n  { }\n  ```\n\n - 生成类型，并指定类型名称，如下示例生成 `Juxtapose.Test.HelloGreeter` 类型\n  ```C#\n  [Illusion(typeof(Greeter), generatedTypeName: \"Juxtapose.Test.HelloGreeter\")]\n  ```\n\n - 生成从IoC容器获取的接口代理类型，如下示例生成 `Juxtapose.Test.IGreeterIllusionFromIoCContainer` 类型（此时Context类需要实现`IIoCContainerProvider`接口，并提供有效的`IServiceProvider`）\n  ```C#\n  [Illusion(typeof(IGreeterFromServiceProvider), generatedTypeName: \"Juxtapose.Test.IGreeterIllusionFromIoCContainer\", fromIoCContainer: true)]\n  ```\n\n------\n\n### 4.3 添加入口点\n在`Main`方法开始处添加入口点代码，并使用指定上下文\n```C#\nawait JuxtaposeEntryPoint.TryAsEndpointAsync(args, GreeterJuxtaposeContext.SharedInstance);\n```\n\n------\n\n#### 到此已完成开发，创建类型`Juxtapose.Test.GreeterAsIGreeterIllusion`的对象，并调用其方法，其实际逻辑将在子进程中运行；\n\n\n## 5. 调试子进程（`Windows`\u0026\u0026`VisualStudio` Only）\n\n现在会自动附加调试器（启动项目需要直接引用`Juxtapose`包，以确保依赖包正确引入）\n\n#### 在代码中打上断点，运行时将会正确命中断点（只在 `VisualStudio2022 17.0.5` \u0026\u0026 `Win11 21TH2` 中进行了测试，理论上是通用）\n\n------\n\n## 6. AOT支持*（net8.0+）\n\n#### 受限于代码生成器不能相互访问生成的代码，AOT支持需要手动编写部分代码\n\n - 声明 JsonSerializerContext\n  框架默认使用 `System.Text.Json` 进行消息的序列化与反序列化，需要手动定义 `JsonSerializerContext` 声明所有类型:\n  ```\n  [JsonSerializable(typeof(global::Juxtapose.Messages.JuxtaposeAckMessage))]\n  [JsonSerializable(typeof(global::Juxtapose.Messages.ExceptionMessage))]\n  [JsonSerializable(typeof(global::Juxtapose.Messages.InstanceMethodInvokeMessage\u003cglobal::Juxtapose.Messages.ParameterPacks.CancellationTokenSourceCancelParameterPack\u003e))]\n  [JsonSerializable(typeof(global::Juxtapose.Messages.DisposeObjectInstanceMessage))]\n  [JsonSerializable(typeof(global::Juxtapose.Messages.CreateObjectInstanceMessage\u003cglobal::Juxtapose.Messages.ParameterPacks.ServiceProviderGetInstanceParameterPack\u003e))]\n  // .........\n  [JsonSourceGenerationOptions(IgnoreReadOnlyProperties = false, IgnoreReadOnlyFields = false, IncludeFields = true, WriteIndented = false)]\n  partial class SampleJsonSerializerContext : JsonSerializerContext {}\n  ```\n\n - 重写 `JuxtaposeContext` 的 `CreateCommunicationMessageCodecFactory` 方法\n  应用前置步骤声明的 `JsonSerializerContext`\n ```\n  partial class SampleJuxtaposeContext : global::Juxtapose.JuxtaposeContext\n  {\n    protected override ICommunicationMessageCodecFactory CreateCommunicationMessageCodecFactory()\n    {\n        var communicationMessageCodec = new DefaultJsonBasedMessageCodec(GetMessageTypes(), LoggerFactory, SampleJsonSerializerContext.Default.Options);\n        return new GenericSharedMessageCodecFactory(communicationMessageCodec);\n    }\n  }\n ```\n\n#### 至此已完成AOT支持；\n\n#### Note: 为方便开发，上述代码已生成在 `Context` 代码中，可从分析器中找到代码，复制后可直接使用；\n#### Note: 已经过有限的测试；\n\n------\n\n## 7. 工作逻辑\n`SourceGenerator`在编译时生成代理类型，封装通信消息。在创建代理类型对象时，会自动创建子进程，并在子进程中创建目标类型的对象，使用命名管道进行进程间通信，使用`System.Text.Json`进行消息的序列化与反序列化。\n\n### 7.1 关键词列表\n|关键字|名称|来源|作用|\n|----|----|----|----|\n|Context|上下文|手动定义|用于承载所有子进程运行的相关信息|\n|Executor|执行器|自动生成|用于运行时消息解析收发，创建对象，执行静态方法等|\n|ParameterPack|参数包|自动生成|将方法参数封装到一个类型中，以便序列化|\n|Illusion|幻象|自动生成|实际使用的类|\n|RealObjectInvoker|真实对象执行器|自动生成|在子进程中接收消息，并进行实际的对象方法调用|\n\n## 更多功能细节参见示例代码\n\n----\n\n### 示例列表\n\n|       项目        |       内容        |\n| ---------------- | ---------------- |\n|SampleLibrary|基于库的使用示例，可由其它程序直接使用|\n|SampleConsoleApp|基于控制台的使用示例，可使用当前程序集生成的类，或使用其他库生成的类|\n|ResourceBasedObjectPool|基于系统资源的动态对象池示例|","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstratosblue%2Fjuxtapose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstratosblue%2Fjuxtapose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstratosblue%2Fjuxtapose/lists"}