In the documentation I found examples of multiple deserializers, but not multiple serializers.
We have a custom serializer that serializes messages to either JSON or XML depending on the message type. Our implementation of IMessageSerializer works fine except the ContentType property.
To me, the cleanest approach looks like implementing two separate IMessageSerializer classes (one for JSON and one for XML) and pick the correct one depending on the message type. But I couldn’t find an example of that anywhere (I thought maybe SerializationDefinition.Configure method could be used for that purpose). I was wondering if this is possible and if so how (some example code would be greatly appreciated)…
Edit: I currently work around this problem by implementing an outgoing message mutator (IMutateOutgoingTransportMessages) that changes the content type header. But I would rather use two separate serializers if possible.
Specifying 2 different serializers is not possible.
I’m curious why you need to do that, since as you mentioned the receivers of the message can have multiple deserializers and hence handle the message accordingly.
The contract of one of the messages is quite old and therefore there isn’t much we can do about that, it has to be serialized into XML. For all other messages we chose to have JSON. I suppose I can change them to XML, but I think I will stick with my workaround for now. I don’t want to deal with XML more than I have to
If I may ask, is there a reason to not support multiple serializers, tehcnically SerializationDefinition could have had an interface that would allow for it, I guess? Maybe no one asked for it before since it’s not something people do often?
It is unusual to be wanting to send messages from one endpoint in two different formats.
You say the message contract is old - I assume the consuming endpoint requires it to be in xml? So the receiving endpoint is one that can’t be changed to deserialize the message from json?
Another option for you, depending on what your logic/trigger is for the xml message to be sent, might be to split the endpoint into 2 endpoints - one for the xml message and the other for the json messages. .
Another option for you, depending on what your logic/trigger is for the xml message to be sent, might be to split the endpoint into 2 endpoints - one for the xml message and the other for the json messages.
That’s right. An endpoint just to send that XML message could be created. I’m not sure about creating an endpoint just for that, but that would be a cleaner solution than overriding content type in a mutator