Hi,
I have a scenario where I am getting an exception when trying to process a message which contains a property of type System.Version.
NServiceBus.MessageDeserializationException: An error occurred while attempting to extract logical messages from incoming physical message 767ceb00-9113-4b3d-a7fc-b0dc00bdea72 —> System.ArgumentOutOfRangeException: Version’s parameters must be greater than or equal to zero. Parameter name: revision
I know why this is happening, in my eyes it is a bit of a shortcoming with .NET. The Version class defaults any Version components not known to -1. So if I have a Version string of “1.2.3” it get’s serialized as {‘Version’:{‘Major’:1,‘Minor’:2,‘Build’:3,‘Revision’:-1} .However, if you explicitly set a version part to -1, the class throws an exception and this is what is happening on deserialization.
Although this is not an NServiceBus issue, I would like to solve it in NServiceBus so it handles this scenario gracefully. We are currently using NewtonsoftSerializer for all serialization/deserialization. My first thought was to write a custom serializer that extends this class so it would try and not pass a -1 to the Version constructor. But because NewtonsoftSerializer inherits SerializationDefinition and not IMessageSerializer as I originally thought, I think I would have to create a class that is essentially a copy + paste of the private class JsonMessageSerializer and then implement my custom functionality on top of it. This is when I stopped and thought that things were getting a little complicated.
The other options I considered and decided against:
- Using a message mutator - This could change any version components from -1 to 0, but that isn’t really correct. If a version component isn’t set, then it shouldn’t be included in my opinion.
- Not using the System.Version class at all and passing “MajorVersion”, “MinorVersion” as separate integer properties etc. While we try to not to pass complex objects in messages to keep coupling to a minimum, I think passing a system class like this is not unreasonable.
So I am wondering, have I misunderstood how serialization/deserialization works in NSB or is there a solution I am overlooking?
Thanks,
Ian