468,765 Members | 809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Problems validating a SOAP request in a SOAP Extension

I am recieving the "The root element is missing" error from my soap extension
while attempting to validate an incoming SOAP message request. I suspect the
problem resides in the ChainStream method but I'm not sure. Below is a
water-downed version of the code that I'm working on. While debugging, I
noticed that if I pass an invalid SOAP request, the schema validation catches
it and throws the error appropriately. However, when I pass in a valid SOAP
request, the root element error is returned but not from the validation
handler. With this, I don't believe the problem is arising from the
XmlValidatingReader process. Any advise on how to resolve this issue is
appreciated.

public class MySolSoapExtension : SoapExtension
{
private Stream inputStream;
private Stream outputStream;
private bool bPostDeserialize;

public override object GetInitializer( Type serviceType )
{
return GetType();
}

public override object GetInitializer( LogicalMethodInfo methodInfo,
SoapExtensionAttribute attribute )
{
return null;
}

public override void Initialize( object initializer )
{
bPostDeserialize = false;
return;
}

public override Stream ChainStream( Stream stream )
{
//This flag will be set to true during the AfterDeserialize handler in
ProcessMessage()
if( !bPostDeserialize)
{
this.inputStream = stream;
this.outputStream = new MemoryStream();
this.inputStream.Position = 0;
return outputStream;
}

else
{
this.inputStream = new MemoryStream();
this.outputStream = stream;
this.outputStream.Position = 0;
return inputStream;
}
}
public override void ProcessMessage(SoapMessage msg)
{
switch(msg.Stage)
{
case SoapMessageStage.BeforeDeserialize:
try
{
// read incoming xml soap message
XmlTextReader tr = new XmlTextReader(new StreamReader(msg.Stream));

// validate xml
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.ValidationType = ValidationType.Schema;

// create a schema collection
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add("http://schemas.xmlsoap.org/soap/envelope/", "envelope.xml");
xsc.Add("http://schemas.xmlsoap.org/soap/encoding/", "encoding.xml");

// load schema collection
vr.Schemas.Add(xsc);

// add validation event handler
vr.ValidationEventHandler += new
ValidationEventHandler(reader_ValidationEventHandl er);
while (vr.Read());
vr.Close();

bPostDeserialize = true;
}
finally
{
// reset stream position
msg.Stream.Position = 0;
}

break;
case SoapMessageStage.AfterDeserialize:
break;
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
break;
}
}

private static void reader_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
throw new SoapException("Schema Error: " + e.Exception.Message, new
XmlQualifiedName("qname"));
}
}
Nov 23 '05 #1
0 2726

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Doe. | last post: by
4 posts views Thread by pepcag | last post: by
1 post views Thread by MarkoH | last post: by
reply views Thread by Matt Wood | last post: by
2 posts views Thread by Frederik Vanderhaegen | last post: by
reply views Thread by Patrick Brunmayr | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.