NServiceBus
NServiceBus copied to clipboard
SimpleJson does not support record types
SimpleJson does not work well together with record types introduced with C# 9.
SimpleJson is used in many places across many downstreams and when it is used to serialize user-defined types (e.g. saga data), it might not be compatible with the user-defined record types. It's not exactly clear in which cases record types work and in which they won't. I've quickly tested both record type syntax on messages for XML and Newtonsoft.Json serializer and they seem to work correctly. The comment on this issue indicates a problem when using record types as part of saga properties.
The problems seems to connect to SimpleJson requirements for an empty default constructor, which is not available when using the positional parameters format, e.g.
public record VehicleInfo(VehicleType Type, string Identifier, string Nationality, IEnumerable<string> VirtualStorageUnitCodes);
This can somewhat be worked around by using the traditional property syntax that allows the definition of an empty ctor, e.g.
public record VehicleInfo
{
public VehicleInfo()
{
}
public VehicleInfo(
VehicleType type,
string identifier,
string nationality,
IEnumerable<string> virtualStorageUnitCodes
)
{
Type = type;
Identifier = identifier;
Nationality = nationality;
VirtualStorageUnitCodes = virtualStorageUnitCodes;
}
public VehicleType Type { get; init; }
public string Identifier { get; init; }
public string Nationality { get; init; }
public IEnumerable<string> VirtualStorageUnitCodes { get; init; }
}
Follow up
- https://github.com/Particular/NServiceBus/issues/6061#issuecomment-1041595031