最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - How to control trimming in dotnet publish with WebAssembly? - Stack Overflow

programmeradmin3浏览0评论

I'm currently working on a small client-side webapp using Avalonia and WebAssembly, specifically (Visual Studio 2022, in C#, with .NET 8.0 and Avalonia 11). I got it to the point it was working (locally) enough to let people start playing with it while I developed it further, but when I uploaded it to GitHub pages it immediately had problems. This is my first web-based development project so I'm not familiar with these issues (I'm a desktop and embedded developer).

The one I'm currently stuck on is that it's saying that it can't find a constructor for a type I load from JSON with Newtonsoft.Json. Based on the searching I've done, it appears that this is due to trimming during the publish process that doesn't play nicely with reflection (prior to this research I didn't even know that the publish process does more aggressive trimming than the build process). I've tried adding TrimmerRootAssembly with the assembly that has the types that are loaded from JSON, but that had no discernible effect. If I turn off trimming with PublishTrimmed dotnet publish no longer produces warnings about trimming issues, but the webapp fails to run for the following reason (in the browser dev console):

[MONO] /__w/1/s/src/mono/mono/metadata/icall.c:7096 dotnet.runtime.js:3:176301 dotnet.native.js:8:18755 Your mono runtime and class libraries are out of sync. dotnet.native.js:8:18755 The out of sync library is: System.Private.CoreLib.dll dotnet.native.js:8:18755 dotnet.native.js:8:18755 When you update one from git you need to update, compile and install dotnet.native.js:8:18755 the other too. dotnet.native.js:8:18755 Do not report this as a bug unless you're sure you have updated correctly: dotnet.native.js:8:18755 you probably have a broken mono install. dotnet.native.js:8:18755 If you see other errors or faults after this message they are probably related dotnet.native.js:8:18755 and you need to fix your mono install first.

Though if possible I'd rather allow trimming but somehow make it not trim the stuff I need for parsing the JSON. I know others have suggested changing JSON libraries, but I'd strongly prefer not to do that at this time given that it's used in a shared library and completely changing how that library works would affect other projects as well (some of which aren't mine).

Does anyone know how to fix or work around this problem?

UPDATE: Tried adding [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] to all the types that are loaded through JSON. No discernible effect.

I'm currently working on a small client-side webapp using Avalonia and WebAssembly, specifically https://github/TheRealQuantam/ZeldaMusicRandomizer (Visual Studio 2022, in C#, with .NET 8.0 and Avalonia 11). I got it to the point it was working (locally) enough to let people start playing with it while I developed it further, but when I uploaded it to GitHub pages it immediately had problems. This is my first web-based development project so I'm not familiar with these issues (I'm a desktop and embedded developer).

The one I'm currently stuck on is that it's saying that it can't find a constructor for a type I load from JSON with Newtonsoft.Json. Based on the searching I've done, it appears that this is due to trimming during the publish process that doesn't play nicely with reflection (prior to this research I didn't even know that the publish process does more aggressive trimming than the build process). I've tried adding TrimmerRootAssembly with the assembly that has the types that are loaded from JSON, but that had no discernible effect. If I turn off trimming with PublishTrimmed dotnet publish no longer produces warnings about trimming issues, but the webapp fails to run for the following reason (in the browser dev console):

[MONO] /__w/1/s/src/mono/mono/metadata/icall.c:7096 dotnet.runtime.js:3:176301 dotnet.native.js:8:18755 Your mono runtime and class libraries are out of sync. dotnet.native.js:8:18755 The out of sync library is: System.Private.CoreLib.dll dotnet.native.js:8:18755 dotnet.native.js:8:18755 When you update one from git you need to update, compile and install dotnet.native.js:8:18755 the other too. dotnet.native.js:8:18755 Do not report this as a bug unless you're sure you have updated correctly: dotnet.native.js:8:18755 you probably have a broken mono install. dotnet.native.js:8:18755 If you see other errors or faults after this message they are probably related dotnet.native.js:8:18755 and you need to fix your mono install first.

Though if possible I'd rather allow trimming but somehow make it not trim the stuff I need for parsing the JSON. I know others have suggested changing JSON libraries, but I'd strongly prefer not to do that at this time given that it's used in a shared library and completely changing how that library works would affect other projects as well (some of which aren't mine).

Does anyone know how to fix or work around this problem?

UPDATE: Tried adding [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] to all the types that are loaded through JSON. No discernible effect.

Share Improve this question edited Feb 17 at 4:36 Justin Olbrantz asked Feb 17 at 3:36 Justin OlbrantzJustin Olbrantz 6494 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

While only a partial answer to the more general question, I figured out a solution to the problem in my circumstance. I had to use the TrimmerRootDescriptor method (the TrimmerRootAssembly method did not work). I added the following to my browser .csproj:

  <ItemGroup>
    <TrimmerRootDescriptor Include="TrimmerRootDescriptor.xml" />
  </ItemGroup>

And then created TrimmerRootDescriptor.xml with:

<linker>
  <assembly fullname="Newtonsoft.Json" />
  <assembly fullname="FtRandoLib" />
</linker>

Where FtRandoLib is the class library that uses Newtonsoft JSON and also defines the types used in JSON deserialization.

发布评论

评论列表(0)

  1. 暂无评论