Schema validation component

In my latest project we had a requirement to validate schemas, In several locations within the process flow schemas needed validation.

Now I know validation can be performed in an xml pipeline, but then again when looking in to the pipeline code using reflection validation happens using an XmlReader against the XSD schema, there are several benefits in writing a custom component.

The XmlReader takes in its constructor an XmlReaderSettings component, the XmlReaderSettings exposes an event we can subscribe to which will trigger if the schema validation fails, This allows to create an ESB fault message(we where using the ESB Exception management portal) without running within a try catch block, which is more preferment.

The XmlReaderSettings uses the XmlSchemaSet object to store the schemas it will validate against which allows the ability to cache the schemas and thus improve performance. Note that XmlSchemaSet is guaranteed to be thread safe only when used as a static field(singleton), all schemas that need validation must be loaded in to the XmlSchemaSet in the static constructor and thus create a caching effect.

The code is available here SchemaValidation.cs

Advertisements

Enumerating context properties

Enumerating the context properties of a BizTalk message can be useful when trying to solve an error in an orchestration.

I wrote the following method to our tracing component that will loop through the context properties of an XLANGMessage.

because the context properties are hidden within the XMessage object in the Microsoft.XLANGs.Core name space, I had to use reflection to unwrap  XMessage in order to expose the GetContextProperties() method.

Important:

This method uses the internal method “Unwrap” in the XMessage object that is NOT intended to be used by user code DO NOT USE this method in a production environment, I use it only for testing purposes and remove the call to the method when finished.

public void WriteMessageProperties(XLANGMessage message)
{
    try
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(Environment.NewLine);
        if (message is Microsoft.XLANGs.Core.MessageWrapperForUserCode)
        {
            Microsoft.XLANGs.Core..MessageWrapperForUserCode mwu = (Microsoft.XLANGs.Core..MessageWrapperForUserCode)message;
            Microsoft.XLANGs.Core.XMessage xmessage = (Microsoft.XLANGs.Core.XMessage)mwu.GetType()
            .GetMethod("Unwrap", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
            .Invoke(mwu, null);
            if (xmessage != null)
            {
                System.Collections.Hashtable ht = xmessage.GetContextProperties();
                Microsoft.XLANGs.Core.XmlQNameTable tbl = new Microsoft.XLANGs.Core.XmlQNameTable(ht);
                foreach (System.Collections.DictionaryEntry dic in tbl)
                {
                    Microsoft.XLANGs.BaseTypes.XmlQName N = (Microsoft.XLANGs.BaseTypes.XmlQName)dic.Key;
                    sb.Append(N.Name + " : " + dic.Value + Environment.NewLine);
                }
            }
        }
        LogEntry entry = new LogEntry();
        entry.Message = sb.ToString();
        entry.Categories = new string[1] { sourceType };
        entry.Severity = System.Diagnostics.TraceEventType.Verbose;
        Logger.Write(entry);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.ToString(), "Error");
    }
    finally
    {
        // Call Dispose on the XLANGMessage object 
        // because the message doesn't belong to the 
        // .NET runtime - it belongs to the MessageBox database 
        message.Dispose();
    }
}