Skip to content

Create a NativeAOT-compatible JsonRpc.AddLocalTargetObject method#1236

Merged
AArnott merged 13 commits intomainfrom
dev/andarno/rpcTargetUpdates
Aug 6, 2025
Merged

Create a NativeAOT-compatible JsonRpc.AddLocalTargetObject method#1236
AArnott merged 13 commits intomainfrom
dev/andarno/rpcTargetUpdates

Conversation

@AArnott
Copy link
Copy Markdown
Member

@AArnott AArnott commented Aug 4, 2025

This provides a NativeAOT safe API for adding local RPC target objects. It adds several API layers to maintain backward compatibility in a non-trim-safe way, then a nearly-as-friendly way to achieve trim/AOT safety but with trimming not being that good, then a slightly more low-level API that achieves great trimmability.
The low-level API requires you to explicitly code up a call to Add for each interface that a class or interface implements. That's a bit tedious, but doable. And in a subsequent change I'll write an interceptor so that this code is written for you automatically, allowing you to call the more friendly APIs and still be optimally trimmed.

This is what NerdbankMessagePackFormatter (#1100) needs in order to support marshalable objects while remaining NativeAOT safe.

AArnott and others added 10 commits July 29, 2025 21:20
Classes can implement new interfaces without that being a binary breaking change. So we shouldn't start failing in those circumstances either.
Interfaces on the other hand cannot add more base types without it being a binary breaking change, so that's ok to leave breaking if incomplete.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new NativeAOT-compatible API for adding local RPC target objects to StreamJsonRpc. The primary purpose is to enable AOT-safe RPC target registration by providing explicit metadata handling instead of relying on dynamic reflection.

Key changes include:

  • Introduction of RpcTargetMetadata class for explicit type metadata management
  • New AddLocalRpcTarget overload accepting RpcTargetMetadata instead of reflection-based types
  • Replacement of dynamic method discovery with pre-computed metadata structures

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/StreamJsonRpc/Reflection/RpcTargetMetadata.cs New core class providing AOT-safe metadata for RPC targets with interface/class analysis
src/StreamJsonRpc/JsonRpc.cs Added new AddLocalRpcTarget overload accepting RpcTargetMetadata parameter
src/StreamJsonRpc/Reflection/RpcTargetInfo.cs Refactored to use RpcTargetMetadata instead of dynamic type analysis
test/StreamJsonRpc.Tests/RpcTargetMetadataTests.cs New test file validating metadata generation for interfaces and classes
test/StreamJsonRpc.Tests/JsonRpcMethodAttributeTests.cs Updated tests to verify new behavior instead of expecting exceptions
test/NativeAOTCompatibility.Test/Program.cs Updated to demonstrate new AOT-compatible API usage
Comments suppressed due to low confidence (1)

test/StreamJsonRpc.Tests/JsonRpcTests.cs:1511

  • This debugging code appears to be accidentally committed. The TraceSource configuration should be removed unless it's intentionally part of the test.
        var rpc = new JsonRpc(streams.Item1, streams.Item2)
        {
            TraceSource = new TraceSource("Loopback", SourceLevels.Verbose)
            {
                Listeners = { new XunitTraceListener(this.Logger) },
            },
        };

Comment thread src/StreamJsonRpc/Reflection/RpcTargetMetadata.cs Outdated
Comment thread src/StreamJsonRpc/Reflection/RpcTargetMetadata.cs
Comment thread src/StreamJsonRpc/Reflection/RpcTargetMetadata.cs
@AArnott AArnott linked an issue Aug 6, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add native AOT-compatible AddLocalRpcTarget equivalent

3 participants