473,587 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Xml.XmlV alidatingReader .set_Validation Type raised InvalidOperatio nException

I am using an XmlValidatingRe ader 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.InvalidO perationExcepti on has occurred
: The operation is not valid due to the current state of the object.
[source = System.Xml]
[method call = set_ValidationT ype]
[stack trace = at
System.Xml.XmlV alidatingReader .set_Validation Type(Validation Type
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 XmlValidatingRe ader vReader = null;
private static XmlSchemaCollec tion xsc = new XmlSchemaCollec tion();
// . . .
StringReader sr = new StringReader(xs dString);
XmlTextReader schemaReader = new XmlTextReader(s r);
xsc.Add(null, schemaReader);
// . . .
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = ValidationType. Schema;
Nov 12 '05 #1
4 2594
This exception would be caused when the ReadState of the validaitng reader
is not set to ReadState.Initi al, 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.goo gle.com...
I am using an XmlValidatingRe ader 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.InvalidO perationExcepti on has occurred
: The operation is not valid due to the current state of the object.
[source = System.Xml]
[method call = set_ValidationT ype]
[stack trace = at
System.Xml.XmlV alidatingReader .set_Validation Type(Validation Type
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 XmlValidatingRe ader vReader = null;
private static XmlSchemaCollec tion xsc = new XmlSchemaCollec tion();
// . . .
StringReader sr = new StringReader(xs dString);
XmlTextReader schemaReader = new XmlTextReader(s r);
xsc.Add(null, schemaReader);
// . . .
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 XmlValidatingRe ader vReader = null;
private static XmlSchemaCollec tion xsc = new XmlSchemaCollec tion();

private OrderValidator( )
{
// Access XSD as an embedded resource in this assembly:
System.Reflecti on.Assembly myAssembly;
myAssembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly();
System.Resource s.ResourceManag er myManager = new
System.Resource s.ResourceManag er("xxx.xxx.xxx .xxx.Order_XSD" ,
myAssembly);
string xsdString =
myManager.GetSt ring("Order_xsd ",System.Global ization.Culture Info.InvariantC u
lture); // this is case-sensitive
StringReader sr = new StringReader(xs dString);
XmlTextReader schemaReader = new XmlTextReader(s r);

// Add XSD to schemaCollectio n
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(XmlTe xtReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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 privateStaticOb ject = new Object();

public Boolean Validates(XmlTe xtReader candidate)
{
Boolean isValid = false;
lock (privateStaticO bject)
{
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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******** ******@TK2MSFTN GP11.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 XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 XmlValidatingRe ader vReader = null;
private static XmlSchemaCollec tion xsc = new XmlSchemaCollec tion();

private OrderValidator( )
{
// Access XSD as an embedded resource in this assembly:
System.Reflecti on.Assembly myAssembly;
myAssembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly();
System.Resource s.ResourceManag er myManager = new
System.Resource s.ResourceManag er("xxx.xxx.xxx .xxx.Order_XSD" ,
myAssembly);
string xsdString =
myManager.GetSt ring("Order_xsd ",System.Global ization.Culture Info.InvariantC u lture); // this is case-sensitive
StringReader sr = new StringReader(xs dString);
XmlTextReader schemaReader = new XmlTextReader(s r);

// Add XSD to schemaCollectio n
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(XmlTe xtReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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 privateStaticOb ject = new Object();

public Boolean Validates(XmlTe xtReader candidate)
{
Boolean isValid = false;
lock (privateStaticO bject)
{
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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.m icrosoft.com> wrote in message
news:OH******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP11.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 XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 XmlValidatingRe ader vReader = null;
private static XmlSchemaCollec tion xsc = new XmlSchemaCollec tion();

private OrderValidator( )
{
// Access XSD as an embedded resource in this assembly:
System.Reflecti on.Assembly myAssembly;
myAssembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly();
System.Resource s.ResourceManag er myManager = new
System.Resource s.ResourceManag er("xxx.xxx.xxx .xxx.Order_XSD" ,
myAssembly);
string xsdString =

myManager.GetSt ring("Order_xsd ",System.Global ization.Culture Info.InvariantC u
lture); // this is case-sensitive
StringReader sr = new StringReader(xs dString);
XmlTextReader schemaReader = new XmlTextReader(s r);

// Add XSD to schemaCollectio n
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(XmlTe xtReader candidate)
{
Boolean isValid = false;
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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 privateStaticOb ject = new Object();

public Boolean Validates(XmlTe xtReader candidate)
{
Boolean isValid = false;
lock (privateStaticO bject)
{
vReader = new XmlValidatingRe ader(candidate) ;
vReader.Schemas .Add(xsc);
vReader.Validat ionType = 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 ApplicationExce ption(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
4236
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 the types of metadata, but the id() function applied to the IDREFS attribute allways returns nothing. I have tried it from xslt and using the...
0
1868
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 against the XmlSchema that I have added to the Schemas collection of the XmlValidatingReader. I also know that the schema is sound, if I comment out the...
2
1797
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. The powers that be have determined that this is acceptable. I am looking for the best way to continue to validate my documents against my...
0
1774
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 System.Xml.Schema.DtdParser.ScanDtdContent() at System.Xml.Schema.DtdParser.ParseDtdContent() at System.Xml.Schema.DtdParser.ParseDocTypeDecl()
5
19576
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return...
0
2033
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 corresponding Web Reference to the Web Site project I get the error listed below. I am able to create other Web Services on the same server and I am able...
2
14367
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 we get the error CPU goes 100% We have been through all we can think of, but have not been able to locate the source to the error. Any...
2
1374
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 <xs:simpleType name="atypename"> <xs:restriction base="xs:token"> <xs:pattern value="\w{1,6}"/>
3
14196
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 transfer file attachments to and from the server. I'm using WSE 2.0 SP3 which is supposed to support FX 2.0, as far as I know. (We will soon move on...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.