Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:26:53+00:00 2026-06-11T09:26:53+00:00

I have an XmlSchema that is instantised as a singleton. public static XmlSchema MessageSchema

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:26:54+00:00Added an answer on June 11, 2026 at 9:26 am

    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 XmlSchema public members, the XmlDocument.Validate() method is. The XmlSchema object contains state information that is not thread safe.

    I changed the MessageShema to a per-thread singleton.

    public static XmlSchema MessageSchema { ... }
    [ThreadStatic]
    private static XmlSchema messageSchema;
    

    It loads the schema a few more times than I’d rather, but it works now. This also meant I could drop the MessageSchemaLock as the value now cannot be accessed by multiple threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML Schema that looks like this: <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:element name=root> <xs:complexType>
I have a webservice that returns a complex value (this is a tree) public
I have this string : <meis xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance uri=localhost/naro-nei onded=flpSW531213 identi=lemenia id=75 lastStop=bendi xsi:noNamespaceSchemaLocation=http://localhost/xsd/postat.xsd xsd/postat.xsd>
I have one xsd file I'd rather not modify (Exceptions.xsd): <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns=http://me.com/Exceptions.xsd targetNamespace=http://me.com/Exceptions.xsd
If I have a schema like this: <?xml version=1.0 encoding=utf-8?> <xs:schema targetNamespace=http://mysticwarlords.kaa/XMLSchema xmlns=http://mysticwarlords.kaa/XMLSchema xmlns:xs=http://www.w3.org/2001/XMLSchema>
I have a Linq-2-XML query that will not work if a google sitemap that
I have a .NET web service that is returning the following: <ArrayOfAddressLocation xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema
I have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I have an SOAP response that looks similar to this: <?xml version=1.0 encoding=UTF-8?> <soapenv:Envelope
I have a ConfigParameter SLSB that reads configuration values from the ejb-jar.xml file. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.