System.Text.Json in SolidWorks Addin (.NET Framework 4.8) gives runtime exception

Programming and macros
Kjara
Posts: 21
Joined: Mon Jul 19, 2021 2:49 am
Answers: 0
x 1

System.Text.Json in SolidWorks Addin (.NET Framework 4.8) gives runtime exception

Unread post by Kjara »

I built a sample Swx Addin in .NET Framework 4.8. I checked that it can be registered, unregistered, and that it runs in SolidWorks,
showing a greeting / goodbye message on addin load / unload.

Then I added a call to System.Text.Json.JsonDocument.Parse(…), which results in the following runtime exception:

Exception has occurred: CLR/System.IO.FileNotFoundException
An exception of type 'System.IO.FileNotFoundException' occurred in System.Text.Json.dll but was not handled in user code:
'The file or assembly "System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" or a dependency could not be found.
The system cannot find the file.


(I translated the message since it is German on my computer, so don’t be confused if the text differs a bit from the original English exception message.)

This problem does NOT occur if I create an application that is not a SolidWorks Addin (e.g. a console application). So the problem must be somewhere in SolidWorks.

What can I do to get rid of it? Using another json parsing library (like Newtonsoft) is no option.

Steps to reproduce:
1. Build the attached project (csproj file and cs file) with “dotnet build”. Building will automatically register the dll in the codebase.
2. Run SolidWorks. The addin will pop up a greeting message (corresponding to line 114 in the cs file), then throw said exception (in line 117). You might need a debugger to see the exception. (I used VSCode to debug.)
3. If you want to unregister the dll, just clean the project with “dotnet clean”.

If you comment out line 117 prior to building, no issues occur.

I used SolidWorks 2021, SP 5.1.
Attachments
SwxAddin using System.Text.Json.zip
csproj and cs file for building (and registering) the swx addin dll
(2.23 KiB) Downloaded 54 times
Kjara
Posts: 21
Joined: Mon Jul 19, 2021 2:49 am
Answers: 0
x 1

Re: System.Text.Json in SolidWorks Addin (.NET Framework 4.8) gives runtime exception

Unread post by Kjara »

I also asked this question on StackOverflow, stating some further thoughts and subquestions:

https://stackoverflow.com/q/72978989/5333340

It already contains a workaround (manually loading the missing assembly) and in the comments some hints to why the issue occurs in the first place.
Post Reply