Path: redb-route/redb.Route/Serialization/XmlMessageSerializer.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Serialization/XmlMessageSerializer.cs
----
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using redb.Route.Abstractions;
namespace redb.Route.Serialization;
///
/// XML message serializer using .
/// Thread-safe, caches instances per type.
///
public sealed class XmlMessageSerializer : IMessageSerializer
{
// Weak per type: a type of an unloadable module (collectible load context) does not stay alive in this cache,
// and the serializer .NET generates for it lives in that same load context.
private static readonly System.Runtime.CompilerServices.ConditionalWeakTable SerializerCache = new();
private readonly XmlWriterSettings _writerSettings;
private readonly XmlReaderSettings _readerSettings;
/// Default writer settings: UTF-8 without BOM, no XML declaration, no indentation.
public static readonly XmlWriterSettings DefaultWriterSettings = new()
{
Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false),
OmitXmlDeclaration = true,
Indent = false,
CloseOutput = false
};
/// Default reader settings.
public static readonly XmlReaderSettings DefaultReaderSettings = new()
{
CloseInput = false,
IgnoreWhitespace = true
};
/// Creates an XML serializer with default settings.
public XmlMessageSerializer()
: this(DefaultWriterSettings, DefaultReaderSettings)
{
}
/// Creates an XML serializer with custom writer and reader settings.
/// Settings for XML output.
/// Settings for XML input.
public XmlMessageSerializer(XmlWriterSettings writerSettings, XmlReaderSettings readerSettings)
{
_writerSettings = writerSettings ?? throw new ArgumentNullException(nameof(writerSettings));
_readerSettings = readerSettings ?? throw new ArgumentNullException(nameof(readerSettings));
}
///
public string ContentType => "application/xml";
///
public byte[] Serialize(T value)
{
var serializer = GetOrCreateSerializer(typeof(T));
using var ms = new MemoryStream();
using (var xw = XmlWriter.Create(ms, _writerSettings))
{
serializer.Serialize(xw, value);
}
return ms.ToArray();
}
///
public T? Deserialize(byte[] data)
{
ArgumentNullException.ThrowIfNull(data);
var serializer = GetOrCreateSerializer(typeof(T));
using var ms = new MemoryStream(data);
using var xr = XmlReader.Create(ms, _readerSettings);
return (T?)serializer.Deserialize(xr);
}
///
public object? Deserialize(byte[] data, Type type)
{
ArgumentNullException.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(type);
var serializer = GetOrCreateSerializer(type);
using var ms = new MemoryStream(data);
using var xr = XmlReader.Create(ms, _readerSettings);
return serializer.Deserialize(xr);
}
private static XmlSerializer GetOrCreateSerializer(Type type)
=> SerializerCache.GetValue(type, static t => new XmlSerializer(t));
}