Uh oh!
There was an error while loading. Please reload this page.
Add source generation of proxies - #1223
Merged
Andrew Arnott (AArnott) merged 91 commits intoJul 23, 2025
Merged
Conversation
…ration-at-compile-time
Uh oh!
There was an error while loading. Please reload this page.
Ryan Toth (RyanToth3)
approved these changes
Jul 23, 2025
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Andrew Arnott (AArnott)
enabled auto-merge (squash)
July 23, 2025 19:14
Ryan Toth (RyanToth3)
approved these changes
Jul 23, 2025
Andrew Arnott (AArnott)
commented
Jul 23, 2025
MemberAuthor
@microsoft-github-policy-service rerun |
Uh oh!
There was an error while loading. Please reload this page.
Andrew Arnott (AArnott)
deleted the
746-client-proxy-generation-at-compile-time
branch
July 23, 2025 19:57
Comment on lines
+58
to
+62
| if (!context.SemanticModel.Compilation.IsSymbolAccessibleWithin(context.TargetSymbol, context.SemanticModel.Compilation.Assembly)) | ||
| { | ||
| // Reported by StreamJsonRpc0001 | ||
| return []; | ||
| } |
There was a problem hiding this comment.
Consider doing this check before doing the work to get known symbols.
This was referenced Jan 22, 2026
This was referenced Jun 8, 2026
This was referenced Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This cuts down on startup time and gets us a significant step closer to being NativeAOT ready.
🌞 Highlights
Startup performance improves because we don't have to reflect over the RPC interface, Ref.Emit ourselves a type that implements that interface, then JIT compile it. The JIT step was immune to ngen because the type doesn't exist at ngen time. Having a source generated implementation over the RPC interface solves all these problems.
NativeAOT is incapable of Ref.Emit and JIT. It has limited reflection capability. So all these perf improvements also gets us closer to full functionality when run under NativeAOT.
📖 Overview
New attributes
JsonRpcContractAttributewhich users should label their RPC interfaces with.JsonRpcProxyMappingAttributewhich is used by the source generator to allow reflection-based discovery of the source generated proxy for a given RPC contract interface.JsonRpcProxyInterfaceGroupAttributewhich predefines sets of RPC interfaces for which a proxy should be source generated so they can be implemented together.ExportRpcContractProxiesAttributeis an assembly-targeted attribute that exposes the generated proxies as public types and optionally forbids (by policy) external assemblies from source generating their own proxies. The forbidding policy will be important for assemblies such as RpcContracts, which reserves the right to add interface members, which would become a binary breaking change in a world of source generated proxies otherwise.A new analyzer that reads
[JsonRpcContract]attributed interfaces and reports diagnostics when they violate our rules for such interfaces (e.g. no properties, methods must use one of a few return types, etc.) as well as uphold the new requirement that such interfaces must be declared with at leastinternalvisibility so that the source generator can emit a proxy class that implements it.This new analyzer also scans
[RpcMarshalable]interfaces and upholds its variant of the rules.A source generator that produces proxy classes that implement those
[JsonRpcContract]interfaces and theJsonRpcProxyMappingAttributeto allow their discovery at runtime.The generated proxy classes are declared as
internal(orpublic) and discovered via the[JsonRpcProxyMappingAttribute]at runtime.A project that compiles against the new StreamJsonRpc library may opt-in to C# interceptor behavior that will rewrite (most) calls to
JsonRpc.Attachto instead call a custom method that directly instantiates the necessary proxy type. This eliminates all reflection and (more importantly) moves the compiled assembly away from theAttachmethods that are attributed as requiring dynamic and unreferenced code which are incompatible with NativeAOT.This interceptor is not on by default because it will cause runtime failures if the requested interfaces have no source generated proxy for them, whereas prior behavior would have generated dynamic proxies for any compatible interface.
Dictionary<string, object?>instead of a custom struct. This avoids the reflection required by dynamic proxies.JsonRpc.Attachcalls are searched for in the consuming project and when the interface type(s) are statically discoverable, we ensure that proxies for those types are source generated (reducing the need forJsonRpcProxyInterfaceGroupAttributeto only those non-analyzable combinations).A diagnostic suppressor to turn off trim/NativeAOT warnings due to Attach calls that the Interceptor will rewrite anyway.
Add and update tests so that the source generator and new proxies are tested in a variety of configurations.
New docs for the analyzers.
Out of scope
These are important items, but it will take a bit more work and I'm eager to merge this so we can show some incremental progress.