I have an XmlSchema that is instantised as a singleton.
public static XmlSchema MessageSchema
{
get
{
lock (MessageSchemaLock)
{
// If this property is not initialised, initialise it.
if (messageSchema == null)
{
// Read XSD from database.
string xsd = Database.Configuration.GetValue("MessageBaseXsd");
using (TextReader reader = new StringReader(xsd))
{
messageSchema = XmlSchema.Read(reader, (sender, e) => {
if (e.Severity == XmlSeverityType.Error) throw e.Exception;
});
}
}
}
// Return the property value.
return messageSchema;
}
}
private static XmlSchema messageSchema = null;
private static readonly object MessageSchemaLock = new object();
This schema is used to validate every document that enters the system. The following method performs the validation.
/// <summary>
/// Validates the XML document against an XML schema document.
/// </summary>
/// <param name="xml">The XmlDocument to validate.</param>
/// <param name="xsd">The XmlSchema against which to validate.</param>
/// <returns>A report listing all validation warnings and errors detected by the validation.</returns>
public static XmlSchemaValidationReport Validate(XmlDocument xml, XmlSchema xsd)
{
XmlSchemaValidationReport report = new XmlSchemaValidationReport();
xml.Schemas.Add(xsd);
xml.Validate((sender, e) => { report.Add(e); });
xml.Schemas.Remove(xsd);
return report;
}
The XmlSchemaValidationReport contains a ‘List’ and some helper methods, nothing that ever sees the XmlSchema object.
When I validate messages on multiple threads, the Validate method fails after the first few messages have been processed. It reports that one of the elements is missing, despite me seeing it clear as day. My test is sending the same message several times, each as a separate XmlDocument. I have double-checked that the MessageSchema property is the only code that ever sets the messageSchema field.
Is the XmlSchema somehow being altered during validation? Why is my validation failing?
Thank you for all of the comments and MiMo’s answer! They led me to a solution.
It seems that while I’m not calling any of the
XmlSchemapublic members, theXmlDocument.Validate()method is. TheXmlSchemaobject contains state information that is not thread safe.I changed the
MessageShemato a per-thread singleton.It loads the schema a few more times than I’d rather, but it works now. This also meant I could drop the
MessageSchemaLockas the value now cannot be accessed by multiple threads.