Is there a local System.Text.Json cache per assembly?

127 views Asked by At

I am experiencing a strange problem with serialization using System.Text.Json in version 6.0.8: serialization time depends on my assembly name.

I have a console app (net48) that only contains the following and exits:

var timer = Stopwatch.StartNew();
var result = JsonSerializer.Serialize("Hello, World!");
timer.Stop();

Console.WriteLine(result);
Console.WriteLine(timer.Elapsed.ToString());

For some reason the time it takes to serialize the string depends on the assembly name. I have somehow "contaminated" an assembly name. If I use that name, then the serialization will take around 4 seconds. For something that is already a string! If I just change the assembly name, the result is some hundred milliseconds. I don't need to recompile - I can take the exact same binary that took 4s to execute, rename it, and it will run in 2-300ms.

I have tried:

  • Moving the app to a different machine: If I move the assembly with the bad name to a different machine, or run it from the Windows sandbox, it runs fast as well. No issue here.

  • Restarting: Didn't make a difference.

  • Using Newtonsoft: I don't experience this issue with Newtonsoft. I have so far only experienced it with System.Text.Json.

  • Looping over the Serialization: It is only the first invokation that is very slow - even if I change the input in every iteration.

  • .NET Core 6.0: Doesn't have this behavior.

  • Use Version 7.0.3: I couldn't reproduce the issue with this version.

Since the issue for this assembly is only observable on machine and it survives a restart, I assume there is some cache on my machine that has become corrupted for that assembly name. But basically I don't know what is going on... Any good ideas?

0

There are 0 answers