BizTalk Web Service: Enum's picking default values automatically and resulting in Xml Schema validation issues.

Posted at: 2/14/2008 at 5:08 PM by saravana

Recently I encountered a serious problem with BizTalk generated web service while doing Xml validation that uses mandatory elements with enumeration.

System Background:

  1. Schemas are exposed as web services using "BizTalk Web Publishing Wizard".
  2. The messages come into BizTalk via Web Service and SOAP adapter.
  3. A custom XML schema validation pipeline component was build, which is capable of returning all the validation errors (the default out of the box BizTalk one only returns the first validation error) in a serializable error collection.
  4. Published messages are picked up by our business process Orchestration, whose first job is to validate it against the schema using the custom pipeline component created in step #3 (by using XLANGPipelineManager.ExecuteReceivePipeline inside an expression shape and capturing exception). If any validation errors are identified, then they are mapped to response message with appropriate error codes.

Our system heavily depends on the schema validation and different error codes it generates. For example, say if a mandatory element called "channel" is not supplied then an error code called INVALID_CHANNEL will be returned, the consumer will kick off different work flow based on the critical error codes. So, it's crucial for us to get the schema validation and correct error codes generation spot on.

Problem:
In our schemas, some of the critical elements are expressed as enums using "derivation-by-restriction" on xs:string. Example: An element named "channel”"(mandatory, min=1) will have enumeration values "Postal", "Email", "Fax".

Sample Valid Message:

image  

Sample Invalid Message (because mandatory "channel" element is missing):

image

If the invalid message is passed to our orchestration as it's, everything will work fine. The orchestration will do the schema validation, will detect the missing channel element and generates the appropriate response message with correct error code (INVALID_CHANNEL). But the problem lies in the web service through which the message travels. When the web service receives the above invalid message it identifies the missing "channel" and substitutes the first enumeration value as default for the missing element "channel". So, by the time the message reaches the orchestration it will look like

image

You can clearly see the problem here. The orchestration won't throw any schema validation exception since the mandatory "channel" element is present but with completely random value. This might have serious implication on the down line business systems.

Solution:
The solution to the problem is quite straight forward. All you need to know is set the "nillable" property of the "channel" element to "true".

image

We are using BizTalk Server 2006, and thanks mainly go to .NET Framework 2.0 "Nullable<T>" type. I assume the problem still persist in BizTalk Server 2004 based solution which runs on top of .NET 1.1

Under the hood:
When we don’t specify the "nillable" property, the default value is "false". This will result in web service code being generated in a normal way as shown below

image 

If you recollect in .NET data types are separated into "value" types and "reference" types. "Reference" types are allocated on the CLR heap and the value types are allocated on the stack. Most of the basic data types like int, char, bool etc fall under "value" type so as Enum and Struct.

A value type cannot be nullable because it has enough capacity to express only the values appropriate for that type; it does not have the additional capacity required to express a value of null.

When the web service deserialize the incoming message it will assign null values to the missing the properties. Since a null value cannot be assigned to the Enum type, by default it puts the first value in the list.

You need to remember, the internal representation of enum's is integers, by default it will start from "0". So, from the web service perspective it's assigning the initial value "0" to the property.

(NOTE: The heading of the post is concentrating on Enum, but you will experience similar problem with basic data types like "int" as well, if the mandatory element of type int is not supplied, after serialization an element with value "0" will automatically be inserted by the web service.)

Effect of assigning nullable = true

When we set the nullable property to true in our schema, the way web service code is generated is slightly different as shown below

image

Nullable<T> Generic structure is new in .NET 2.0 to accommodate the usage of null on value types. See this link for more information http://msdn2.microsoft.com/en-us/library/b3h38hb0.aspx

Now the web service serialization won't assing the default value to the "channel" element. It will be null instead, so when the message gets serialized it, the "channel" element wont be there.

Losing Messages:

During this exercise I also identified a strange behaviour, which is not acceptable on an enterprise system. If the serialization fails for some reason, say for example you put a wrong value for the enum, Example <channel>Pigeon</channel> or even put wrong case for example <channel>postal</channel> ('p' is lower case). There won't be any errors or warning raised by the web service and message won’t be submitted to BizTalk. (NOTE: This is applicable only to one-way web service operations).

Nandri!
Saravana

Tags: |  Categories: BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(16) | Comments RSS

Comments

Thursday, February 14, 2008 6:27 PM
Richard
The only way to can see that you've "lost" the message is by looking at the IIS logs and noticing that a post occured to the SOAP endpoint URL.
Thursday, February 14, 2008 7:52 PM
Ryan CrawCour
Nice post! thanks for that; i've just gone through the pain of something very similar will .NET picking default values for enums.

As a matter of interest how does your orchestration go about validating the schema? You mentioned a pipeline component, in which case is it really your orchestration that does the validation?

Would you be so kind as to share your pipeline component with us as I am sure we all have similar problems with the standard MS XML receive pipeline.

Thanks
Friday, February 15, 2008 10:39 AM
Saravana Kumar
Hi Richard,
Yeah that's correct you'll see an entry in the IIS log saying you received the message, but the HTTP status will be 200 (Successful) which is not correct.

Regards,
Saravana
Friday, February 15, 2008 10:44 AM
Saravana Kumar
Hi Ryan,

The pipeline component we build was a standard BizTalk pipeline component, just an extension to the out of the box XmlValidation component.

But we don't use it directly in the receive port, since that will just result in the message being suspended.

We want the response message with correct error codes for each validation errors back to the consumer in the response message.

So, we call the same pipeline component inside the orchestration using BizTalk 2006 new functionality. See this link for more information msdn2.microsoft.com/en-us/library/aa562035.aspx and map the error codes to the response message using a standard map.

Regards,
Saravana
Tuesday, December 09, 2008 10:16 AM
trackback
Trackback from 鸡尾虾的壳

Biztalk-Web Service集成对消息Xsd值类型节点的特殊处理
Friday, July 09, 2010 4:34 AM
Shena Cherrington
dat komt alleen maar omdat de temperatuur stijgt... its just hard
Thursday, July 29, 2010 6:10 AM
Postal Uniforms
Great piece. I'll book mark that for later.
Monday, August 02, 2010 2:28 AM
lenen
Lenen Zonder BKR Toetsing Lenen zonder BKR toetsing stijgt in populariteit op het Internet. Veel mensen met een zogeheten BKR notatie, die toch geld willen lenen zijn op zoek naar ...
Thursday, August 05, 2010 1:45 AM
Seattle Limos
I'm a huge fan of your website and I read it regularly.  Keep up the excellent work!
Saturday, August 07, 2010 3:52 PM
Cam Girls
Hi, I read a couple of your great posted posts and wanted to ask if you would be interested in exchanging pages?
Saturday, August 07, 2010 4:19 PM
Cam Sex
I like the blog, but could not find how to subscribe to receive the updates by email.
Monday, August 09, 2010 4:55 AM
Bike Cycling
I think I like what you got going on here check back in a few. thanks.
Tuesday, August 10, 2010 7:49 AM
hypotheek
Hoeveel kan ik lenen? (hypotheek). Wat worden mijn maandlasten? (hypotheek) ... Hoeveel hypotheek heb ik nodig? Hoe hoog is de boete die ik nu zou moeten
Thursday, August 12, 2010 5:43 AM
tv on pc
This is getting a bit more subjective, but I much prefer the Zune Marketplace. The interface is colorful, has more flair, and some cool features like 'Mixview' that let you quickly see related albums, songs, or other users related to what you're listening to. Clicking on one of those will center on that item, and another set of "neighbors" will come into view, allowing you to navigate around exploring by similar artists, songs, or users. Speaking of users, the Zune "Social" is also great fun, letting you find others with shared tastes and becoming friends with them. You then can listen to a playlist created based on an amalgamation of what all your friends are listening to, which is also enjoyable. Those concerned with privacy will be relieved to know you can prevent the public from seeing your personal listening habits if you so choose.
Wednesday, August 18, 2010 3:17 AM
Air force one
Many thanks for mentioning your thoughts and time into the stuff you create. I'm bookmarking this for furture reference.
Friday, September 03, 2010 11:23 AM
RayBan Sunglasses
awesome post

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading