XmlSchemaSet thread schema validation / security error?

Good afternoon,

The XML schema validation snippet works fine in the / q and am development environment, but gives some odd validation results in Production. A common suspect would be that the code is unsafe for streaming and that additional loading of the Production script fixes the error.

The exact scenario is as follows. Note that validated XML:

<mssql:spExecute type="ResultSet" xmlns:mssql="urn:namespace"> <mssql:actor>IPASS</mssql:actor> <mssql:connection>ConnectionString</mssql:connection> <mssql:storedProcedure>dbo.RedFox</mssql:storedProcedure> </mssql:spExecute> 

Over the course of a full day, about 300 performances (from> 2M) will throw the following exception:

  System.Xml.Schema.XmlSchemaValidationException
 The 'http://www.w3.org/2000/xmlns/:mssql' attribute is not declared.

The schema validator seems to be complaining about the namespace declaration.

The code structure is as follows:

  • There is a static instance of XmlSchemaSet;
  • XmlSchemaSet instance initialization is thread-safe;
  • Each worker thread uses the same XmlSchemaSet;
  • Validation is performed during an XmlSerializer.Deserialize () call using the XmlReader with the XmlReaderSettings initialized with ValidationType.Schema.

Any thoughts on what might cause the validation to strangle the namespace declaration?

+4
source share
3 answers
  • Is this what you describe as the result of a controlled experiment or observation of a production environment? If the latter, try playing it in a controlled setting to make sure that some other error is not the real cause.

  • The documentation for XmlSchemaSet states:

    "Any instance members do not guarantee thread safety."

    This includes property reads and read-only methods. Therefore, we cannot assume that using a XmlSchemaSet only XmlSchemaSet will be thread safe. Until Microsoft provides an implementation that explicitly allows the sharing of the same compiled representation of a circuit between threads, the only safe thing is not to share.

  • OTOH seems logical to share one compiled immutable XmlSchemaSet for checking multiple instances of XmlDocument (or XmlReader ). This is a very reasonable scenario, so I don’t understand why this is clearly not allowed and documented.

    (Update: this seems to be explicitly guaranteed in the standard Java XML library . Why is this not true in .NET?)

+4
source

This can be fixed in .NET 4.0 but not documented.

I ran into the same error in the web production service. The error is similar and random during peak activity: "Element xxx not declared." However, in my case, we simply validate the XML using the cyclic Read command, rather than serializing the object.

The service is built / running under .NET 3.5, and in the development environment, I can easily replicate this error by starting 5 threads and simultaneously making a service call. The error occurs about 1 out of 10 requests.

When I sat down to solve this problem, the first thing I did was upgrade the service to .NET 4.0. Once I have done this, I can no longer produce this error. I increased the number of threads and removed the throttling of the service - there are still no errors.

While current documentation still indicates that the operations of the XmlSchema instance are not guaranteed by the thread, there may have been some structural change between 3.5 and 4.0, which improves thread safety for operations such as reading / checking on XmlSchema.

+2
source

I had the same problem. It appears that XML validation is not the read-only operation that this article refers to:

http://blogs.msdn.com/xmlteam/archive/2009/04/27/xmlschemaset-thread-safety.aspx

Holding the lock, he solved the problem for me.

+1
source

All Articles