BizTalk Does Not Validate My Message?!?!
The ability to rigidly define the content of a data field is an essential element in any structured data definition. It is no surprise therefore one can define data constraints on elements and attributes in a schema through the means of various facets, such as enumerations, min length, regular expressions, and many more. The implementation of facets in a schema provides a large amount of value in terms of the data aspect of the contract to be used in an integration. Whether the integration is WCF-based or not, this data contract must be adhered to and messages that do not adhere to this contract must not be processed.
There is, however, no value in defining the data constraints if these constraints can not be enforced at the time the data is received, as allowing "invalid" data to be processed will invariably lead to errors in the processing of that data. It is for this reason that validating a message against its schema is oftentimes a necessary requirement. Some might say that this validation should be performed by default, and any messages that do not validate should be rejected as early in the processing of the message as possible. BizTalk does not do this by default.
At this point you may be saying what do you mean "BizTalk does not validate my message?" ... hence, the title of this post.
The point is that BizTalk CAN validate your message, but it does not do so by default. One of the reasons for this is flexibility: you may prefer to do the validation later in your process, perhaps in an orchestration, where you can handle the validation errors in your own way; or you may want the receive pipeline to do the validation and have BizTalk route the failed message. This decision is not made for you by BizTalk ... your solution design will determine the appropriate implementation.
In this post I will present two ways in which you can get BizTalk to fully validate your message against its schema as early in the receiving process as possible: at the receive pipeline.
Using the XMLReceive Pipeline
When using the standard XMLReceive pipeline in a receive location you can click on the ellipsis button to the right of the pipeline selection box to display the "Configure Pipeline" window. The screenshot below shows how the XMLReceive pipeline makes use of the XML disassembler pipeline component and the Party resolution pipeline component. When first setting up the receive location the ValidateDocument and DocumentSpecNames properties are False and blank respectively. In this default configuration the XMLReceive pipeline will try and match the received XML document's target namespace and root node name to the published schemas in BizTalk, and the received document's structure will be validated against the schema's structure. No data validation is performed on the received document, however.
By setting the ValidateDocument property to True the XML disassembler pipeline component will be instructed to validate the data contained in the received document as well. Setting this property to true also requires that at least one DocumentSpecName is also provided, as the XML disassembler uses the schema specified by the fully qualified assembly name in the DocumentSpecNames property to identify the schema with which to perform the validation. With these properties set, if the receive location processes a message that fails validation an entry like the one below will be logged to the event log.
While this approach achieves the objective it falls short for an ideal solution, because:
- The XML disassembler pipeline component has logic built into it to identify the matching schema for a received message, so why does it not use this schema to perform validation? In other words, I would like to be able to change the Validate flag to true, without needing to specify a value for the DocumentSpecNames property.
- Every time the schema changes (possibly due to version changes) the developer will need to remember to go and change the configuration of the XMLReceive pipeline to ensure that the correct fully qualified assembly name is stored in the DocumentSpecNames property.
- If I have multiple receive locations that each need to do schema validation the previous task would be compounded as I would need to remember to set the properties for each receive location.
This leads on to the second method to ensure that BizTalk validates the content of the received message: a custom pipeline that can be used in any receive location to do full schema validation, without requiring any additional configuration.
Creating and Using the XMLValidatingReceive Pipeline
The concept behind the XMLValidatingReceive pipeline is to have a generic pipeline that one can use in any project, and in any receive location where the received document's structure and data content needs to be validated against an existing schema. As the standard functionality of matching a received document with a deployed schema is still required, the XMLReceive pipeline is an excellent starting point. As the XMLReceive pipeline uses the XML disassembler and Party resolution pipeline components, the XMLValidatingReceive pipeline will need to use these two pipeline components as a starting point as well. In addition to these components, the XMLValidatingReceive pipeline will also make use if the XML validator pipeline component.
To create the XMLValidatingReceive pipeline, create a new receive pipeline. I created this pipeline in a generic BizTalk solution and project, so that I could use it for any solution, at any customer. In the new receive pipeline, add the XML disassembler, XML validator and Party resolution pipeline components, as per the screenshot below.
There is no need to change any of the default properties for any of these components. The pipeline can now be deployed and used in any BizTalk application. A message processed by this pipeline will now use the XML disassembler's schema resolution functionality and it will validate the structure of the document against the resolved schema. The XML validator component will then use the resolved schema name to validate the received document's data content, in accordance with any facets defined in the schema. Where a received document fails validation, the same error as in the previous method is shown (as below), except that the pipeline referred to will now be the new custom pipeline.
The result is a common pipeline that can be used in any receive location, and which does not require any configuration of pipeline properties to ensure that the received document is full validated. At the same time, this pipeline does not require any changes to be effected in the event of a change to a schema.
In conclusion, don't assume that BizTalk will enforce those facets you diligently applied to your schema. You need to instruct BizTalk to validate a received message against the data constraints you define in your schema, and there are two simple ways in which this can be achieved. If it is a regular requirement to ensure that the received document is fully validated against the schema, then create a custom XMLValidatingReceive pipeline that can be used in any BizTalk development project.