473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

System.Xml.XmlValidatingReader.set_ValidationType raised InvalidOperationException

I am using an XmlValidatingReader which uses an XSD for xml
validation. The code has been performing reliably for months.
Yesterday it failed for the first time with the following exception:

Inner exception of type System.InvalidOperationException has occurred
: The operation is not valid due to the current state of the object.
[source = System.Xml]
[method call = set_ValidationType]
[stack trace = at
System.Xml.XmlValidatingReader.set_ValidationType( ValidationType
value)

As I said, this code has been quite some time. It has been tested and
is now in a production system in a critical code path. When it failed
it caused a significant financial transaction to fail. I am wondering
if anyone has ever seen this behaviour or has any recommendations.

The offending code is as follows:

private static XmlValidatingReader vReader = null;
private static XmlSchemaCollection xsc = new XmlSchemaCollection();
// . . .
StringReader sr = new StringReader(xsdString);
XmlTextReader schemaReader = new XmlTextReader(sr);
xsc.Add(null, schemaReader);
// . . .
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
Nov 12 '05 #1
4 2570
This exception would be caused when the ReadState of the validaitng reader
is not set to ReadState.Initial, which imples that the validating reader was
moved ahead( via a call to Read() ) before setting the ValidationType
property.

Could you share your original code and data files?

Thanks.
Zafar
"Jesse Elve" <je********@t4g.com> wrote in message
news:23**************************@posting.google.c om...
I am using an XmlValidatingReader which uses an XSD for xml
validation. The code has been performing reliably for months.
Yesterday it failed for the first time with the following exception:

Inner exception of type System.InvalidOperationException has occurred
: The operation is not valid due to the current state of the object.
[source = System.Xml]
[method call = set_ValidationType]
[stack trace = at
System.Xml.XmlValidatingReader.set_ValidationType( ValidationType
value)

As I said, this code has been quite some time. It has been tested and
is now in a production system in a critical code path. When it failed
it caused a significant financial transaction to fail. I am wondering
if anyone has ever seen this behaviour or has any recommendations.

The offending code is as follows:

private static XmlValidatingReader vReader = null;
private static XmlSchemaCollection xsc = new XmlSchemaCollection();
// . . .
StringReader sr = new StringReader(xsdString);
XmlTextReader schemaReader = new XmlTextReader(sr);
xsc.Add(null, schemaReader);
// . . .
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;

Nov 12 '05 #2
Zafar:

This has turned out to be a very interesting bug. I will be very pleased if
anyone will agree with me that the only possible cause is due to multiple
threads accessing the function simultaneously. (My supervisor thinks that
I'm jumping to this conclusion too hastily!)

In any event, the exception was raised on the final line of the following
code excerpt (!):

vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;

Note that this is an _exact_ excerpt. As you can see, there is _no_
possibility that "read" is called before the "ValidationType" is set, as the
reader has just been instantiated 2 lines up!

This becomes more interesting when the full class is considered:

-------------------------------------------------------------------------
public class OrderValidator
{
private static XmlValidatingReader vReader = null;
private static XmlSchemaCollection xsc = new XmlSchemaCollection();

private OrderValidator()
{
// Access XSD as an embedded resource in this assembly:
System.Reflection.Assembly myAssembly;
myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Resources.ResourceManager myManager = new
System.Resources.ResourceManager("xxx.xxx.xxx.xxx. Order_XSD",
myAssembly);
string xsdString =
myManager.GetString("Order_xsd",System.Globalizati on.CultureInfo.InvariantCu
lture); // this is case-sensitive
StringReader sr = new StringReader(xsdString);
XmlTextReader schemaReader = new XmlTextReader(sr);

// Add XSD to schemaCollection
xsc.Add(null, schemaReader);
}

/// <summary>
/// Singleton instance via .NET-Framework-managed singleton semantics
/// </summary>
public static readonly OrderValidator Instance = new OrderValidator();

/// <summary>
/// Validation method
/// </summary>
/// <param name="candidate">fresh XMLTextReader (unread!)</param>
/// <returns>true for valid XML or throws an exception for
failure</returns>
public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name :
String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ? vReader.Value :
String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
return isValid;
}

}
-------------------------------------------------------------------------

There were numerous factors which made it logical to design this class as a
Singleton, chiefly the size and complexity of the XSD. Once instantiated,
the singleton instance of this class does service validation requests from
multiple threads.

The only explanation that I can come up with is that 2 contending threads
are in the "Validates" method simultaneously. I'm very open to any other
explanation!

In the meantime, I have already implemented the following modifications
based on my "thread contention" theory:

--------------------------------------------------------------------------

// object used for method-level locking
private static Object privateStaticObject = new Object();

public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
lock (privateStaticObject)
{
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name :
String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ? vReader.Value :
String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
}
return isValid;
}

--------------------------------------------------------------------------

Based on a previous incident, however, our architect thinks I'm too quick to
jump to a "thread contention" conclusion. If anyone has any other
interpretations or suggestions please let me know!
Nov 12 '05 #3
Definitely looks like a thread contention issue since your original code has
vReader as a static variable and you have no locks around the code. Locking
the code for the entire validation period is expensive and basically loses
all the benefits of using multiple threads. The simple answer is for vReader
not to be a static member variable.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Jesse Elve" <je********@t4g.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
Zafar:

This has turned out to be a very interesting bug. I will be very pleased if anyone will agree with me that the only possible cause is due to multiple
threads accessing the function simultaneously. (My supervisor thinks that
I'm jumping to this conclusion too hastily!)

In any event, the exception was raised on the final line of the following
code excerpt (!):

vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;

Note that this is an _exact_ excerpt. As you can see, there is _no_
possibility that "read" is called before the "ValidationType" is set, as the reader has just been instantiated 2 lines up!

This becomes more interesting when the full class is considered:

-------------------------------------------------------------------------
public class OrderValidator
{
private static XmlValidatingReader vReader = null;
private static XmlSchemaCollection xsc = new XmlSchemaCollection();

private OrderValidator()
{
// Access XSD as an embedded resource in this assembly:
System.Reflection.Assembly myAssembly;
myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Resources.ResourceManager myManager = new
System.Resources.ResourceManager("xxx.xxx.xxx.xxx. Order_XSD",
myAssembly);
string xsdString =
myManager.GetString("Order_xsd",System.Globalizati on.CultureInfo.InvariantCu lture); // this is case-sensitive
StringReader sr = new StringReader(xsdString);
XmlTextReader schemaReader = new XmlTextReader(sr);

// Add XSD to schemaCollection
xsc.Add(null, schemaReader);
}

/// <summary>
/// Singleton instance via .NET-Framework-managed singleton semantics
/// </summary>
public static readonly OrderValidator Instance = new OrderValidator();

/// <summary>
/// Validation method
/// </summary>
/// <param name="candidate">fresh XMLTextReader (unread!)</param>
/// <returns>true for valid XML or throws an exception for
failure</returns>
public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name :
String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ? vReader.Value : String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
return isValid;
}

}
-------------------------------------------------------------------------

There were numerous factors which made it logical to design this class as a Singleton, chiefly the size and complexity of the XSD. Once instantiated,
the singleton instance of this class does service validation requests from
multiple threads.

The only explanation that I can come up with is that 2 contending threads
are in the "Validates" method simultaneously. I'm very open to any other
explanation!

In the meantime, I have already implemented the following modifications
based on my "thread contention" theory:

--------------------------------------------------------------------------

// object used for method-level locking
private static Object privateStaticObject = new Object();

public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
lock (privateStaticObject)
{
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name :
String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ? vReader.Value : String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
}
return isValid;
}

--------------------------------------------------------------------------

Based on a previous incident, however, our architect thinks I'm too quick to jump to a "thread contention" conclusion. If anyone has any other
interpretations or suggestions please let me know!

Nov 12 '05 #4
Thanks,

In retrospect, that seems obvious!
"Dare Obasanjo [MSFT]" <da***@online.microsoft.com> wrote in message
news:OH**************@TK2MSFTNGP15.phx.gbl...
Definitely looks like a thread contention issue since your original code has vReader as a static variable and you have no locks around the code. Locking the code for the entire validation period is expensive and basically loses
all the benefits of using multiple threads. The simple answer is for vReader not to be a static member variable.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Jesse Elve" <je********@t4g.com> wrote in message
news:ul**************@TK2MSFTNGP11.phx.gbl...
Zafar:

This has turned out to be a very interesting bug. I will be very pleased
if
anyone will agree with me that the only possible cause is due to
multiple threads accessing the function simultaneously. (My supervisor thinks that I'm jumping to this conclusion too hastily!)

In any event, the exception was raised on the final line of the following code excerpt (!):

vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;

Note that this is an _exact_ excerpt. As you can see, there is _no_
possibility that "read" is called before the "ValidationType" is set, as

the
reader has just been instantiated 2 lines up!

This becomes more interesting when the full class is considered:


-------------------------------------------------------------------------
public class OrderValidator
{
private static XmlValidatingReader vReader = null;
private static XmlSchemaCollection xsc = new XmlSchemaCollection();

private OrderValidator()
{
// Access XSD as an embedded resource in this assembly:
System.Reflection.Assembly myAssembly;
myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Resources.ResourceManager myManager = new
System.Resources.ResourceManager("xxx.xxx.xxx.xxx. Order_XSD",
myAssembly);
string xsdString =

myManager.GetString("Order_xsd",System.Globalizati on.CultureInfo.InvariantCu
lture); // this is case-sensitive
StringReader sr = new StringReader(xsdString);
XmlTextReader schemaReader = new XmlTextReader(sr);

// Add XSD to schemaCollection
xsc.Add(null, schemaReader);
}

/// <summary>
/// Singleton instance via .NET-Framework-managed singleton semantics
/// </summary>
public static readonly OrderValidator Instance = new OrderValidator();

/// <summary>
/// Validation method
/// </summary>
/// <param name="candidate">fresh XMLTextReader (unread!)</param>
/// <returns>true for valid XML or throws an exception for
failure</returns>
public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name :
String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ? vReader.Value :
String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
return isValid;
}

}
-------------------------------------------------------------------------

There were numerous factors which made it logical to design this class

as a
Singleton, chiefly the size and complexity of the XSD. Once
instantiated, the singleton instance of this class does service validation requests from multiple threads.

The only explanation that I can come up with is that 2 contending threads are in the "Validates" method simultaneously. I'm very open to any other explanation!

In the meantime, I have already implemented the following modifications
based on my "thread contention" theory:


--------------------------------------------------------------------------

// object used for method-level locking
private static Object privateStaticObject = new Object();

public Boolean Validates(XmlTextReader candidate)
{
Boolean isValid = false;
lock (privateStaticObject)
{
vReader = new XmlValidatingReader(candidate);
vReader.Schemas.Add(xsc);
vReader.ValidationType = ValidationType.Schema;
try
{
while (vReader.Read()){}
isValid = true;
}
catch(Exception e)
{
string message = "Validation failed: ";
message += "Element name: " + (vReader.Name != null ? vReader.Name : String.Empty) + ". ";
message += "Element value: " + (vReader.Value != null ?

vReader.Value :
String.Empty) + ". ";
throw(new ApplicationException(message, e));
}
}
return isValid;
}

--------------------------------------------------------------------------

Based on a previous incident, however, our architect thinks I'm too

quick to
jump to a "thread contention" conclusion. If anyone has any other
interpretations or suggestions please let me know!


Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Microsoft News | last post by:
Hi all. Does id() xpath function work in System.XML? I have built dtd, schema, xdr for a simple xml which includes attributes definded as ID and IDREFS. Validation is occurring properly for all...
0
by: Nick Finch | last post by:
Hi, I wonder if anyone else has come across this. The code below is very simple however I cannot get the ValidationHandler to be raised. I am testing with xml that I know will not validate...
2
by: matthew.schneider | last post by:
I have a defined XML Schema, and XML documents containing data that I validate against it. Periodically, I will have a data document that will contain extra elements that are not part of my schema....
0
by: Juan Galdeano | last post by:
Hi, I'm working on an ONIX project and when I try to validate or read XML files C# gives me this exception: System.IndexOutOfRangeException at System.Xml.XmlScanner.ScanDtdContent() at...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
0
by: nicomp | last post by:
I created a Web Service: I imported System.Data.SqlClient so I could access SQL server tables programmatically. The web service builds and deploys with no problems. When I try to add the...
2
by: job | last post by:
In a sharepoint setup using smartpart to load our user controls using enterprise blocks (data) we are getting some strange errors (logged to the event log). We dont get the error all the time. When...
2
by: Phil Hobgen | last post by:
Hi, I am using the XmlValidatingReader Class in VS.Net 2003 (targeting dotNet v1.1) to validate an xml message against a set of schemas. Within the schema a type is defined as follows ...
3
by: Joseph Geretz | last post by:
System.InvalidOperationException: WebServiceBindingAttribute is required on proxy classes. My environment: Visual Studio 2005, targeting FX 2.0; I've developed a Web Service which uses DIME to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.