Skip to content

LaunchDarkly.ClientSDK 5.3.0 fails in trimmed applications #90

Open

Description

@GaryHughes

Describe the bug
If I create a trimmed application the SDK fails due to the use of reflection based JSON serialisation.

To reproduce
Create a new .NET project and enable trimming in the project file.

true

Integrate the LaunchDarkly API as per your sample applications, when LDClient.Init() is called the following exception will be raised.

2025-04-04 11:22:48.981 +11:00 [LaunchDarkly.Sdk] INFO: Starting LaunchDarkly Client 5.3.0.0 built with target framework net7.0
2025-04-04 11:22:49.034 +11:00 [LaunchDarkly.Sdk] INFO: Network availability is now True
Unhandled exception. System.InvalidOperationException: Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.
at LaunchDarkly.Sdk.Internal.Concurrent.AsyncUtils.WaitSafely(Func`1 taskFn, TimeSpan timeout)
at LaunchDarkly.Sdk.Client.LdClient.Start(TimeSpan maxWaitTime)
at LaunchDarkly.Sdk.Client.LdClient.Init(Configuration config, Context initialContext, TimeSpan maxWaitTime)
at Program.

$(String[] args) in /Users/ghughes/Developer/CurlAgents/LaunchDarkly/Program.cs:line 6

Expected behavior
The LDClient.Init() method returns without throwing this exception.

Logs
See above.

SDK version
Client 5.3.0

Language version, developer tools
.NET 8.0.

OS/platform
Tested on macOS but exception is not platform specific.

Activity

  1. tanderson-ld commented on Apr 7, 2025

    @tanderson-ld
    Contributor

    Hi @GaryHughes. We see this sometimes in other ecosystems that use reflection around the communication protocol. Is there a way for you to disable trimming for a subset of packages/classes? That may be faster and more guaranteed to get you unblocked. I am not familiar with the flexibility of the trimming configuration for .NET.

  2. GaryHughes commented on Apr 7, 2025

    @GaryHughes
    Author

    Hi @tanderson-ld disabling trimming in general is not possible, our software has tight size constraints that preclude this. I'll look at the options for excluding your code but our experience with that technique has not been great. Replacing reflection based serialisation with source generators is quite straight forward, there is a good explainer here if you haven't seen it https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation.

  3. justindbaur commented on Sep 26, 2025

    @justindbaur
    Contributor

    Hi @tanderson-ld I just made this in a sister repo about the warnings I am seeing from code in that repo. Just like @GaryHughes I am seeing trim warnings about the server SDK.

    Trim analysis warning IL2026: LaunchDarkly.Sdk.Json.LdJsonSerialization.SerializeObject<T>(!!0): Using member 'System.Text.Json.JsonSerializer.Serialize<T>(T,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
    AOT analysis warning IL3050: LaunchDarkly.Sdk.Json.LdJsonSerialization.SerializeObject<T>(!!0): Using member 'System.Text.Json.JsonSerializer.Serialize<T>(T,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
    Trim analysis warning IL2026: LaunchDarkly.Sdk.LdValue.ToJsonString(): Using member 'System.Text.Json.JsonSerializer.Serialize<LdValue>(LdValue,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
    AOT analysis warning IL3050: LaunchDarkly.Sdk.LdValue.ToJsonString(): Using member 'System.Text.Json.JsonSerializer.Serialize<LdValue>(LdValue,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.
    

    It's technically 4 warnings but it's really just 2 areas of code for the server SDK. Should I create a new issue for the server sdk or would tracking it in this issue be fine.

    Would LaunchDarkly welcome a PR addressing these issues? Even though I don't use the client SDK I'd more than love to fix both at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions