Replies: 1 comment
-
|
To read messages in xml format:
public sealed class SystemXmlSerializer : IMessageSerializer
{
/// <inheritdoc />
public byte[] Write(Envelope envelope)
{
if (envelope.Message is null) throw new InvalidOperationException("Envelope message is null");
var serializer = new XmlSerializer(envelope.Message.GetType());
using var writer = new StringWriter();
serializer.Serialize(writer, envelope.Message);
return Encoding.UTF8.GetBytes(writer.ToString());
}
/// <inheritdoc />
public object ReadFromData(Type messageType, Envelope envelope)
{
if (envelope.Data is null) throw new InvalidOperationException("Envelope data is null");
var raw = Encoding.UTF8.GetString(envelope.Data);
var serializer = new XmlSerializer(messageType);
using var reader = new StringReader(raw);
return serializer.Deserialize(reader) ?? throw new InvalidOperationException("Message deserialized as null");
}
/// <inheritdoc />
public object ReadFromData(byte[] data)
{
throw new NotSupportedException("System.Xml.Serialization requires a known message type");
}
/// <inheritdoc />
public byte[] WriteMessage(object message)
{
var serializer = new XmlSerializer(message.GetType());
using var writer = new StringWriter();
serializer.Serialize(writer, message);
return Encoding.UTF8.GetBytes(writer.ToString());
}
/// <inheritdoc />
public string ContentType { get; } = "application/xml";
}
builder.Host.UseWolverine(options =>
{
...
options.AddSerializer(new SystemXmlSerializer());
...
});
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to figure out how to read messages from RabbitMQ sent by a third-party service in xml format.
I need to create a DTO class for this message. For example:
But how can I use a serialization other than JSON?
Something like
UseSystemTextJsonForSerializationbut for concrete queue:Beta Was this translation helpful? Give feedback.
All reactions