Skip to content

Support trimmed/AOT applications by removing implicit dependency on reflection-based System.Text.Json serialization #116

Description

@davidatwhiletrue

Summary

When consuming Casper.Network.SDK from a .NET file-based application, JSON serialization fails with:

Reflection-based serialization has been disabled for this application.
Either use the source generator APIs or explicitly configure the
'JsonSerializerOptions.TypeInfoResolver' property.

The failure originates from RpcMethod.Serialize(), which assumes reflection-based System.Text.Json serialization is available.

Why this matters

Modern .NET applications (including trimmed, Native AOT, and some file-based app scenarios) may disable reflection-based JSON serialization by default. As a result, the SDK cannot be used without consumers explicitly changing their application configuration.

A reusable SDK should ideally not depend on this application-level setting.

Proposed solution

Preferred (long-term)

Adopt System.Text.Json source generation (JsonSerializerContext) for all request and response types used by the SDK.

Benefits:

  • Supports trimmed applications.
  • Supports Native AOT.
  • Removes the SDK's dependency on reflection-based serialization.
  • Improves startup performance.
  • Follows Microsoft's recommended approach for modern .NET libraries.

Possible short-term workaround

Where JsonSerializerOptions are created, configure a DefaultJsonTypeInfoResolver when no resolver has been supplied by the application.

Reproduction

  1. Create a .NET file-based application.
  2. Reference the SDK using #:project.
  3. Invoke an RPC method such as GetAccountInfo().
  4. Observe the exception during request serialization:
Reflection-based serialization has been disabled for this application.
Either use the source generator APIs or explicitly configure the
'JsonSerializerOptions.TypeInfoResolver' property.

Additional notes

Although this was first encountered from a .NET file-based app, the underlying issue is not specific to file-based apps. It can affect any application that disables reflection-based System.Text.Json serialization (for example, trimmed or Native AOT applications).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions