-
Notifications
You must be signed in to change notification settings - Fork 162
Description
Hello!
We are using this package for a recent project and we were looking for supporting AoT. However, we quickly noticed the heavy usage of reflection in particular in the proxies and the dynamic assembly, regardless if the target uses or not an interface, or if we hardcode the method using AddLocalRpcMethod()
. The only benefit of keep using reflection is because I see is that we can dynamically add new targets at runtime but even that use case, according to the docs, is not encouraged after the listener starts, hence why it is guarded by that exception/flag.
It would be nice to have a Roslyn source generator as an external package which allow people which choose to use the reflection approach without breaking anything, and optionally generate the proxy types at build time and embed in the output assembly. We do that a lot in Microsoft Orleans for all the serializer types and invokers.
Would a PR be accepted to add such package? If so, what should I look in the current codebase for the desired generated types?
The approach I would take would be:
- Create a source generator package/project
StreamJsonRpc.CodeGen
as a Roslyn incremental source generator - Introduce a new attribute
[JsonRpcTarget]
which can be used in classes. - At build time, scan the classes which have that attribute and follow the same discovery process done Today by the reflection code, like look for the
[JsonRpcMethod]
, then other public methods without it, ignore private, etc. - With the discovered types, generate the code as partial and append to the assembly
- When the target is added, the generated code would be there so no reflection needed
This would allow people to AoT their applications. If the user decide to use the dynamic target registration along with the source generated, then, at runtime, we would have the bootstrapping benefits on the source generated classes but at the same time add targets dynamically. If the dynamic target doesn't have generated code, it fallback to the reflection emit approach, which obviously wouldn't work in AoT scenarios.
Please let me know if this is something you guys would accept as a PR and if so, where I should look for the desired generated output so I can quickly get into it and submit the PR.
Thanks!