XSD validation for WCF services

Service Station, by Aaron Skonnard

Syndication

Those who know me are familiar with my fondness for XSD validation in the context of Web services. I wrote about it quite a bit for ASMX and shipped some sample validation extensions that made it easy to turn on XSD or business rules (via XPath assertions) validation with custom attributes.
 
I wanted to see how hard/easy it would be to do something similar -- enable XSD validation -- in WCF. They didn't ship a validation switch (for which I've longed) as part of the service model so if you're interested in applying XSD validation you'll have to use a technique similar to the one I use in this sample.
 
You'll find my custom XsdValidationBehavior sample here.
 
It implements IDispatchMessageInspector and does most of the work in AfterReceiveRequest. The behavior uses the WsdlExporter to produce the XSD schemas from the service contract type and uses the generated schemas during validation. The trickiest thing to get right was creating a new Message object for the dispatcher to use after I had consumed the one they handed to me. That part turned out to be a bit more tedious than I would have liked, but it's possible that I'm missing something and there's an easier way.
 
In order to test the sample, add the behavior to your service's behaviors collection manually (as shown here), or you can write a custom attribute or configuration element to enable it declaratively.
 
using (ServiceHost host = new ServiceHost(typeof(SampleService)))
{
    host.Description.Endpoints[0].Behaviors.Add(
        new XsdValidationBehavior());
    host.Open();
    ...
 
Disclaimer: I threw this sample together quickly during a course I was teaching and it has not been tested. I'm providing it to help jump-start those interested in doing something similar.
 

Posted Apr 20 2006, 03:15 PM by Aaron Skonnard
Filed under: ,

Comments

Kirk Allen Evans' Blog wrote Web Service Geek? Go Read Pluralsight's blogs
on 04-20-2006 6:11 PM
I like to write in my blog, but I hardly read other blogs anymore.  In fact, I don't have an aggregator...
dominick wrote re: XSD validation for WCF services
on 04-21-2006 6:23 AM
excellent!

cheers,
dominick
Robbie G wrote re: XSD validation for WCF services
on 04-24-2006 5:23 AM
Hi Aaron,

I implemented a bespoke version of your validation extension for .net 1.1 and we recently upgraded it to 2.0. There were a few "features" we had to iron out - specifically when using a shared xsd file imported across multiple wsdl files, and supporting multiple validation errors instead of barfing on the first one to come along; finally we raised custom soap exceptions containing a serialized collection in the detail rather than throwing a basic System.Exception

Anyways, thanks for the code, with these issues ironed out it works a charm
Abha Jain wrote re: XSD validation for WCF services
on 05-01-2006 7:23 PM
Hi Aaron,
Did you find a better way to modify the request? We have a requirement where we need to change teh message request to another method (action) or just change the parameters of the method, I get this error when I try to use the explained way - "This message can not support the operation because it has already been copied."
Will Appreciate your input.
Thanks,
Abha
Tony Fryer wrote re: XSD validation for WCF services
on 03-16-2007 6:22 PM
Ran into the "This message can not support the operation because it has already been copied." too. Eventually figured out a way around it:

--------------------------------------
' create a bufferedcopy of the request
Dim bufferedCopy As MessageBuffer
bufferedCopy = request.CreateBufferedCopy(Integer.MaxValue)

' get a copy from the buffer
Dim messageCopy As System.ServiceModel.Channels.Message
messageCopy = bufferedCopy.CreateMessage()

' do some processing with messagecopy . . .

'finally replace the request with a fresh copy from the bufferedcopy
request = bufferedCopy.CreateMessage()
----------------------------------

note: The very act of creating the bufferedcopy 'burns' the request that's why we have to work with, and replace it with, a copy from the buffered (which is nice and clean)
João Pedro Martins wrote re: XSD validation for WCF services
on 07-30-2007 2:18 AM
doesn't support xs:any either...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?