With the release of .NET Core 3.0, everyone needs to start thinking about upgrading their existing .NET Core 2.2 apps right away. Not only because .NET Core 3.0 has a bunch of performance gains that you’ll undoubtedly want to take advantage of, but also because support for .NET Core 2.2 ends mid December. Microsoft’s new, quick release schedule forces your hand to upgrade.
At first, that seems annoying because no one really likes spending a couple of days wading through the upgrade path documentation and regression testing their entire application, but actually the upgrades are good. Obviously, as I stated before, you get the performance gains, but Microsoft’s security team is constantly fine tuning the security of their framework so that you and I don’t have to think about it. That’s the best reason for upgrading, in my opinion – getting the security updates.
Upgrade
I was tasked with upgrading my team’s code from 2.2 –> 3.0, and overall it went pretty smoothly. There are a couple of changes to the way that applications are initialized, but following the upgrade documentation proved to be pretty straightforward.
JSON Serializer Gotchas
The main issue that I’ve had is with .NET Core 3.0’s new JSON serializer. Previously, .NET Core just used Json.NET, but now with the other performance gains that Microsoft has made with Span<T> they were able to build a faster serializer that doesn’t even need to allocate memory. Faster execution and smaller footprint sound good to me, but it doesn’t work exactly like the Json.NET serializer I’m used to. Fortunately, the changes required are pretty simple and straightforward:
Parameterless Constructor
In order to deserialize JSON in to your C# object, the serializer needs to create the object and then populate the properties. If there isn’t a parameterless constructor, the serializer can’t easily make the object, and thus can’t populate the properties. This must’ve worked in Json.NET somehow anyway, because the one or two DTOs that I had where a parameterless constructor was not present worked just fine before the upgrade, but afterwards did not.
Properties, not fields
I always have to look up the difference between a field and a property in a C# object. I guess its one of those things that you just get used to using the right one at the right time and forget their proper names (Related: did you know that the inline-if operator ? is called a ternary operator? Yeah, I forgot too).
Anyway, as the linked StackOverflow post shows:
The JSON serializer will only serialize / deserialize properties, so if your object only has fields in it, the new serializer will just give you an empty JSON object.
Properties Gotta Have a Setter
Ok, so this might be obvious, but your properties have to have a { set; } in order to be populated by the serializer. If they don’t, they are effectively read only, which isn’t great for deserialization.
Tuples ain’t it
I had my enum controller which provided a key/value pair representation of a C# enum for my javascript client to use, but it was returning a C# 7 tuple, which doesn’t properly map with the new JSON serializer. This is because the data members are fields, not properties.
Its easy enough to just create a class with “Key” and “Value” properties on it to map the tuple, but it is an extra step that previously wasn’t required.
Really, there’s not a ton of issues I ran in to with .NET Core 3.0. I haven’t had a change to see if the performance is noticeably different quite yet, but the prospect of having things built in to the framework that don’t require external dependencies, to me, seems like a good thing.
YADNC3JSG–Yet Another .NET Core 3.0 JSON Serializer Gotcha – Darchuk.NET
[…] I wrote about several “gotchas” in the new JSON serializer that is built in to .NET Core 3.0. Another one has cropped up, but it is different enough […]