Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trimming support #2813

Open
HavenDV opened this issue Feb 9, 2023 · 3 comments
Open

Trimming support #2813

HavenDV opened this issue Feb 9, 2023 · 3 comments

Comments

@HavenDV
Copy link

HavenDV commented Feb 9, 2023

When trying to publish an application with <PublishTrimmed>true</PublishTrimmed>, this library produces the following:

  Newtonsoft.Json.dll: [IL2104] Assembly 'Newtonsoft.Json' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries

I guess it doesn't support net6.0/net7.0 trimming - https://aka.ms/dotnet-illink/libraries

@elgonzo
Copy link

elgonzo commented Feb 9, 2023

Any feature-rich reflection-based serializer or library (like Newtonsoft.Json/Json.NET) is a struggle regarding trimming.

As the documentation you linked to states:

Sometimes the existing design of an API will render it mostly trim-incompatible, and you may need to find other ways to accomplish what it is doing. A common example is reflection-based serializers. In these cases, consider adopting other technology like source generators to produce code that is more easily statically analyzed.

However, all is not lost. If you want to keep using Newtonsoft.Json while keeping trimming enabled, you will have to exclude any of the types (or their assemblies) that your application can potentially (de)serialize through Newtwonsoft.Json from trimming: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options (But probably it is a good idea to first think about whether migration to System.Text.Json is feasible for you, which would save you from this particular trimming problem.)

@HavenDV
Copy link
Author

HavenDV commented Feb 9, 2023

I guess in order to eliminate this warning, we need to include <IsTrimmable>true</IsTrimmable> but specify one of the properties:

But I'm not sure if this will set any metadata for this to be distributed via the NuGet package

@alexyakunin
Copy link

alexyakunin commented Nov 23, 2023

An easy fix would be to add [DynamicDependency(...)] on one of key methods (which is called no matter what - e.g. one of type initializers) to make sure every type needed for JSON deserialization stays in place even with <TrimMode>full</TrimMode>.

Right now Newtonsoft.JSON doesn't work when fully trimmed, and it seems in our specific case it's the only library in this state now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants