Tuesday, August 22, 2006
Xml Schema Validation in WCF and Web Services - a Discussion
I recently read the excellent Windows Communication Foundation HandsOn by Craig McMurty - it is an excellent read as it is practical and goes through the code at a level that lets you fill in the detail and concentrates on the code that makes WCF an absolutely amazing product. The more i have read, more more interested i have got with one of the most exciting developments to come out of Redmon for a while. If you have ASMX and particularly WSE experience, then you'll be set to start, but WCF offers a full solution and is even extensible to allow you to add many customizations.
However, one particular part of the discussion caught my attention. It came in chapter 3 and was under the title "Xml Fetish". I do agree that it is easy to have everything in Xml and go way beyond the actual requirements, but Craig (or whoever wrote that particular chapter!) says:
"... control over how data is represented in XML is generally of no use to software developers.."
The preceding sections portray a view of Schema based contract development in a relatively negative light, with a preference for software developers caring only about the interface for communication. There is also the mention of interoperability, with DataSet being a particular example, but the .Net DataSet type is in no way related to Xml Schema or even contract first development - indeed many a software developer in the past (and future) adds a DataSet as a parameter and never worries about its representation on the wire or interop - hence hitting problems that wouldn't not necessarily happened with an Xml Schema standards based definition of the types in the contract (i even remember being at Chris Sells Web Services DevCon in 2003 where a standards based DataSet was loosely discussed).
I also read an MSDN article that hinted about these issues without really mentioning any [1] :
"There are several more sophisticated XML Schema concepts you can apply when using XmlSerializer, but using these more advanced features can often lead to interoperability problems across frameworks."
I'm not sure whether these issues relate to Xml Schema (a W3C standards), or some proprietary data embedded in the Schema. I'm sure there are
issues as the author of the article writes excellent articles, but there is just no detail of what these are. Further, in the same article, one can read:
"The result is great flexibility. You're free to define new data contracts in either XML Schema or .NET code and you can easily convert to the other. If you're working in a situation where the XML Schema definitions already exist and you need to support them, you can simply start with svcutil.exe. If you're defining new contracts and need to quickly, start by writing class definitions."
There is an obvious grey area in here - where you don't "need to start quickly", but don't have existing schemas. If you could go back and forwards between .Net and Xml Schema, then one may argue "whatever you are most confortable with". However, Xml Schema is inherently more powerful than the .Net types for defining contracts, mainly because you get some level of business validation on all platforms - in .Net you really only get the Interface.
As a simple example, consider a user ID. Perhaps you have a restriction that the user id is at least 3 charcaters and at most 20 characters. Your contract may accept a .Net "int" type, but that leaves the possible values that can be sent to be ANY valid .Net integer. With Xml Schema, you easily get validation that the value is within the contraints defining the length of the user id. Xml Schema can provide quite complex validation rules and through patterns, regular expressions can be used.
So why do this? Well, if you have a number of clients calling your services, it would be good to ensure information has at least gone through some basic validation before it hits your server. It's very similar to how client scripting helps in web browsers - not as a last resort as validation is still done on the server, but exceptions for example are expensive to raise on the server, so you can get a performance improvement here alone. There are a host of reasons why this would also be useful at the server too, such as providing an external validation before hitting your core internal services and so on.
If Microsoft allowed you to decorate your properties and so on with some equivalent of the configuration validation attributes [2], you may be able to get something close to what is available in Xml Schema - at least at a primitive level - the DataContractSerializer MaxItemsInObjectGraph uses exactly this technique.
At a basic level, would it not make sense for Microsoft's WCF to provide a behavior that could be switched on to says "validate against schema"?
My view at the moment (and hopefully i can get some feedback on this) is that using Xml Schema for contract based development offers some fairly hefty benefits, with pretty much zero work. In fact, the only reason there IS work is that the Microsoft web services frameworks require you to do quite a bit of work to USE your Schemas for validation (certainly outwith the skills of your casual web service developer). Please see my net post for some implementation details and samples in WSE 3.0 and WCF.
At the moment i am working with a set of services within a financial organization that is called from a Java service and a Win32 C++ application, the latter passing pure soap messages. In both these cases Xml Schema validation is useful as the actual call is expensive only to discover your "city" is not a valid entry (and so on).
I'd like to hear arguments and examples of why Schema should be so difficult to turn on and then perhaps i will start to understand these problems - in my *simple* examples, other than the code to perform the validaition itself, i ran into no problems, so i'm open to suggestions!
[1] http://msdn.microsoft.com/msdnmag/issues/06/08/ServiceStation/default.aspx
[2] http://msdn2.microsoft.com/en-us/library/system.configuration.configurationvalidatorbase.aspx
However, one particular part of the discussion caught my attention. It came in chapter 3 and was under the title "Xml Fetish". I do agree that it is easy to have everything in Xml and go way beyond the actual requirements, but Craig (or whoever wrote that particular chapter!) says:
"... control over how data is represented in XML is generally of no use to software developers.."
The preceding sections portray a view of Schema based contract development in a relatively negative light, with a preference for software developers caring only about the interface for communication. There is also the mention of interoperability, with DataSet being a particular example, but the .Net DataSet type is in no way related to Xml Schema or even contract first development - indeed many a software developer in the past (and future) adds a DataSet as a parameter and never worries about its representation on the wire or interop - hence hitting problems that wouldn't not necessarily happened with an Xml Schema standards based definition of the types in the contract (i even remember being at Chris Sells Web Services DevCon in 2003 where a standards based DataSet was loosely discussed).
I also read an MSDN article that hinted about these issues without really mentioning any [1] :
"There are several more sophisticated XML Schema concepts you can apply when using XmlSerializer, but using these more advanced features can often lead to interoperability problems across frameworks."
I'm not sure whether these issues relate to Xml Schema (a W3C standards), or some proprietary data embedded in the Schema. I'm sure there are
issues as the author of the article writes excellent articles, but there is just no detail of what these are. Further, in the same article, one can read:
"The result is great flexibility. You're free to define new data contracts in either XML Schema or .NET code and you can easily convert to the other. If you're working in a situation where the XML Schema definitions already exist and you need to support them, you can simply start with svcutil.exe. If you're defining new contracts and need to quickly, start by writing class definitions."
There is an obvious grey area in here - where you don't "need to start quickly", but don't have existing schemas. If you could go back and forwards between .Net and Xml Schema, then one may argue "whatever you are most confortable with". However, Xml Schema is inherently more powerful than the .Net types for defining contracts, mainly because you get some level of business validation on all platforms - in .Net you really only get the Interface.
As a simple example, consider a user ID. Perhaps you have a restriction that the user id is at least 3 charcaters and at most 20 characters. Your contract may accept a .Net "int" type, but that leaves the possible values that can be sent to be ANY valid .Net integer. With Xml Schema, you easily get validation that the value is within the contraints defining the length of the user id. Xml Schema can provide quite complex validation rules and through patterns, regular expressions can be used.
So why do this? Well, if you have a number of clients calling your services, it would be good to ensure information has at least gone through some basic validation before it hits your server. It's very similar to how client scripting helps in web browsers - not as a last resort as validation is still done on the server, but exceptions for example are expensive to raise on the server, so you can get a performance improvement here alone. There are a host of reasons why this would also be useful at the server too, such as providing an external validation before hitting your core internal services and so on.
If Microsoft allowed you to decorate your properties and so on with some equivalent of the configuration validation attributes [2], you may be able to get something close to what is available in Xml Schema - at least at a primitive level - the DataContractSerializer MaxItemsInObjectGraph uses exactly this technique.
At a basic level, would it not make sense for Microsoft's WCF to provide a behavior that could be switched on to says "validate against schema"?
My view at the moment (and hopefully i can get some feedback on this) is that using Xml Schema for contract based development offers some fairly hefty benefits, with pretty much zero work. In fact, the only reason there IS work is that the Microsoft web services frameworks require you to do quite a bit of work to USE your Schemas for validation (certainly outwith the skills of your casual web service developer). Please see my net post for some implementation details and samples in WSE 3.0 and WCF.
At the moment i am working with a set of services within a financial organization that is called from a Java service and a Win32 C++ application, the latter passing pure soap messages. In both these cases Xml Schema validation is useful as the actual call is expensive only to discover your "city" is not a valid entry (and so on).
I'd like to hear arguments and examples of why Schema should be so difficult to turn on and then perhaps i will start to understand these problems - in my *simple* examples, other than the code to perform the validaition itself, i ran into no problems, so i'm open to suggestions!
[1] http://msdn.microsoft.com/msdnmag/issues/06/08/ServiceStation/default.aspx
[2] http://msdn2.microsoft.com/en-us/library/system.configuration.configurationvalidatorbase.aspx
http://stevenR2.com
... a brief history
Site XML Feed
www.flickr.com |
Posts By Date
Profiles
- my taghop.org web
- taghop linkblogs
- todo list
- who i know
- flickr photos
- delicious links
- evdb events
- my things
- odeo subscriptions
NeighBloggers
taghop
Also made in Scotland
- Tarmac (John Loudon MacAdam)
- McIntosh Coat (Charles McIntosh)
- James Clerk Maxwell
- David Livingstone
- Television (John Logie Baird)
- BBC (Lord Reith)
- Kelvin Temperatures (William Thomson)
- RADAR (Sir Robert Alexander Watson-Watt)
- Steam Engine (James Watt)
- Americanism (John Witherspoon)
- Telephone (Alexander Graham Bell)
- Penicillin (Sir Alexander Fleming)
- Patron Saint of Ireland (Saint Patrick)
- Charles Rennie Mackintosh
- Celtic FC (Jock Stein)
- Treasure Island (Robert Louis Stevenson)
- Paddle Steamer (William Symington)
- Encylopaedia Britannica (William Smellie)
- Las Palmas Observatory (Charles Piazzi Smyth)
- The Bank of England (William Paterson)
- Logarithms & the Decimal Point (John Napier)
- The World Cup (Sir Thomas Lipton)
- MoreOver.com (David Galbraith)
- Blackboard (James Pillans)
- Liverpool FC (Bill Shankly)
- Iron Plough (James Small)
- Robinson Crusoe (Alexander Selkirk)
- Helium (Sir William Ramsay)
- Sociology (Adam Ferguson)
- Harry Potter
- Landspeed Record (Richard Noble)
- Hot Blast Oven (James Beaumont Neilson)
- Coal-Gas Lighting (William Murdock)
- Prime Minister of Canada (Sir John Alexander MacDonald)
- The Bicycle (Kirkpatrick Macmillan)
- Reflecting Telescope (James Gregory)
- The World's Worst Poet (William Topaz McGonagall)
- Geology (James Hutton)
- Carnegie Mellon (Andrew Carnegie)
- Carnegie Institution (Andrew Carnegie)
- Grandfather of the United States (Robert Dinwiddie)
- Universal Standard Time (Sir Sandford Fleming)
- Latent Heat & Carbon Dioxide (Joseph Black)
- James Bond (Sean Connery)
- Rob Stewart
- Auld Lang Syne (Robert Burns)
- Billy Connolly
- Annie Lennox
- U.S. Navy (John Paul Jones)
- Chariots of Fire (Eric Henry Liddell)
- Cure for Scurvy (James Lind)
- Tea Bags (Sir Thomas Lipton)
- Vacuum flask (Sir James Dewar)
- Postage Stamp (James Chalmers)
- Clerk Cycle Gas Engine (Sir Dugald Clerk)
- Cure for Malaria (George Cleghorn)
- Cure for Malaria (George Cleghorn)
- Groundskeeper Willie
- Peter Pan (Sir James Barrie)
- Kaleidoscope (Sir David Brewster)
- Toronto Globe (George Brown)
- Sherlock Holmes (Sir Arthur Conan Doyle)
- Graham's Law (Thomas Graham)
- The Wind in the Willows (Kenneth Grahame)
Release 2.0
Release 1.0
What I know
I Read
Archives
- July 17, 2005
- July 18, 2005
- July 19, 2005
- July 20, 2005
- July 21, 2005
- July 22, 2005
- July 27, 2005
- July 28, 2005
- August 01, 2005
- August 02, 2005
- August 03, 2005
- August 04, 2005
- August 07, 2005
- August 08, 2005
- August 09, 2005
- August 10, 2005
- August 11, 2005
- August 12, 2005
- August 13, 2005
- August 17, 2005
- August 19, 2005
- August 22, 2005
- August 24, 2005
- August 25, 2005
- August 27, 2005
- August 29, 2005
- August 30, 2005
- September 01, 2005
- September 02, 2005
- September 03, 2005
- September 04, 2005
- September 21, 2005
- September 22, 2005
- September 23, 2005
- September 30, 2005
- October 04, 2005
- October 06, 2005
- October 11, 2005
- October 14, 2005
- October 25, 2005
- October 27, 2005
- November 02, 2005
- November 08, 2005
- November 10, 2005
- November 12, 2005
- November 14, 2005
- November 16, 2005
- November 22, 2005
- December 02, 2005
- December 07, 2005
- December 23, 2005
- December 30, 2005
- January 02, 2006
- January 10, 2006
- January 11, 2006
- January 12, 2006
- January 14, 2006
- January 15, 2006
- January 16, 2006
- January 19, 2006
- January 20, 2006
- January 24, 2006
- January 25, 2006
- January 26, 2006
- January 30, 2006
- February 07, 2006
- February 08, 2006
- February 09, 2006
- February 20, 2006
- February 22, 2006
- February 23, 2006
- February 24, 2006
- February 27, 2006
- February 28, 2006
- March 01, 2006
- March 06, 2006
- March 08, 2006
- March 10, 2006
- March 13, 2006
- March 22, 2006
- March 24, 2006
- March 28, 2006
- March 29, 2006
- March 30, 2006
- March 31, 2006
- April 02, 2006
- April 06, 2006
- April 07, 2006
- April 13, 2006
- April 20, 2006
- April 26, 2006
- April 27, 2006
- April 28, 2006
- April 29, 2006
- April 30, 2006
- May 01, 2006
- May 02, 2006
- May 03, 2006
- May 04, 2006
- May 05, 2006
- May 07, 2006
- May 08, 2006
- May 10, 2006
- May 11, 2006
- May 15, 2006
- May 16, 2006
- June 02, 2006
- June 05, 2006
- June 06, 2006
- June 09, 2006
- June 11, 2006
- June 12, 2006
- June 13, 2006
- June 14, 2006
- June 20, 2006
- June 24, 2006
- June 26, 2006
- June 27, 2006
- June 29, 2006
- June 30, 2006
- July 01, 2006
- July 03, 2006
- July 08, 2006
- July 10, 2006
- July 12, 2006
- July 13, 2006
- July 25, 2006
- July 28, 2006
- August 01, 2006
- August 02, 2006
- August 05, 2006
- August 07, 2006
- August 08, 2006
- August 15, 2006
- August 22, 2006
- August 24, 2006
- August 27, 2006
- September 06, 2006
- September 07, 2006
- September 08, 2006
- September 11, 2006
- September 13, 2006
- September 14, 2006
- September 15, 2006
- September 21, 2006
- September 25, 2006
- October 02, 2006
- October 03, 2006
- October 25, 2006
- November 01, 2006
- November 10, 2006
- November 14, 2006
- November 15, 2006
- November 16, 2006
- November 17, 2006
- November 18, 2006
- November 20, 2006
- November 21, 2006
- November 29, 2006
- November 30, 2006
- December 08, 2006
- December 09, 2006