BizTalk开发系列(二十七) 异常管理中的数据编码

更多内容请查看:BizTalk动手实验系列目录

                      BizTalk 开发系列

     在BizTalk的异常管理解决方案中。大部分是通过订阅相关的升级属性来接收消息,并在自定义的流程或发送端口进行处理。但不管怎样,一般会定义统一的错误消息Schema,这样不仅可以让我们通过异常信息快速的查找错误。还可以方便的跟不同的系统或存储方式做映射。

      虽然在BizTalk内部唯一使用Xml文档,但BizTalk所能接受的消息不单单只有XML文档。RAR,ZIP,WMA,WMV等等都是BizTalk可以接收的消息。这就给BizTalk的异常管理造成了一定的麻烦,因为原消息一般会包含到统一的异常消息里。如果消息在产生异常之前就已经被拆装为相应的XML文档的话则可以很方便的借用XMLDocument对像的OuterXML属性获得原消息的消息体。但是如果消息是在Adapter或pipeline出错的话则可能消息往往还没有被处理成XML。

       在捕获异常消息的时候通常将使用XMLDocument对像进行捕获,但如果直接使用XMLDocumentObject.OuterXML属性获取消息的话则会产生类似如下的错误:

 Data at the root level is invalid. Line 1, position 1 Excepion

     

      因些需要对异常消息进行重新编码。在BizTalk中的所有消息都是XLANGMessage类型的。XLANGMessage类提供与不同数据类型进行转换的功能。如下代码所示,我们先将消息转为Stream,再从Stream专为 String类型。 这样我们就可以得到消息的文本内容了。

public static string GetMsgSourceString(XLANGMessage message)
        
{
            
string sourceString = string.Empty;
            Stream sourceStream 
= null;
            StreamReader streamReader 
= null
            
try
            
{
                sourceStream 
= (Stream)message[0].RetrieveAs(typeof(Stream));
                streamReader 
= new StreamReader(sourceStream,Encoding.UTF8);
                sourceString 
= streamReader.ReadToEnd();                
                
return sourceString;
            }

            
catch (System.Exception ex)
            
{
                System.Diagnostics.EventLog.WriteEntry(
"CBCYE.Exception""Failure to get source from message :" + message.Name, System.Diagnostics.EventLogEntryType.Error);
                
return sourceString;
            }

            
finally
            
{
                
if (null != sourceString)
                
{
                    sourceString 
= null;
                }
 

                
if (null != sourceStream)
                
{
                    sourceStream 
= null;
                }
 

                
if (null != streamReader)
                
{
                    streamReader 
= null;
                }

            }
 

        }
 

 

    但如果原消息是RAR,ZIP等格式的数据话就又不一样了。虽然通过该方法也可以进行编码。但是你看到会是类似下面图的数据(ZIP文件转成String的效果)。文档中有乱码不方便保存和阅读而且很容易使数据无法恢复。

image

    不过问题总会有解决办法的,我们可以使用在Internet中应用非常广泛的Base64编码[1]将数据进行重新编码。在C#中可以使用Convert.ToBase64String与Convert.FromBase64String实现byte[]与string(Base64编码)之间的转换。

public static string GetMsgSourceBase64(XLANGMessage message)
        
{
            
string sourceString = string.Empty;
            Stream sourceStream 
= null;
            StreamReader streamReader 
= null;
            
byte[] buffer = null;
            
try
            
{
                sourceStream 
= (Stream)message[0].RetrieveAs(typeof(Stream));
                streamReader 
= new StreamReader(sourceStream, Encoding.UTF8);
                sourceString 
= streamReader.ReadToEnd(); 

                System.Text.ASCIIEncoding accessEncoding 
= new ASCIIEncoding();
                buffer 
= accessEncoding.GetBytes(sourceString);
                sourceString 
= Convert.ToBase64String(buffer);
                
return sourceString;
            }

            
catch (System.Exception ex)
            
{
                System.Diagnostics.EventLog.WriteEntry(
"CBCYE.Exception""Failure to get source from message :" + message.Name, System.Diagnostics.EventLogEntryType.Error);
                
return sourceString;
            }

            
finally
            
{
                
if (null != sourceString)
                
{
                    sourceString 
= null;
                }
 

                
if (null != sourceStream)
                
{
                    sourceStream 
= null;
                }
 

                
if (null != streamReader)
                
{
                    streamReader 
= null;
                }
 

                
if (null != buffer)
                
{
                    buffer 
= null;
                }

            }

        }
 

     经过转换之后同样的Zip文件在文本中存储的数据变成了下图所示.这样的ASCII码字符就不用担心是用Unicode还是UTF-8编码存储。现在绝大多数的语言都是支持Base64的加密与解密的。因此查看与还原原消息也变得很简单了。

image

    最后关于怎么存储异常消息体可以看具体的需求,如果BizTalk接收和发送的的就都是Formatted XML的话就不需要编码了,如果还有Txt,CSV之类的平面的文件的话可以直接返回String,方便查看。 最后如果是比较复杂的集成环境就需要使用Base64对数据进行统一编码。还可以使用InfoPath等客户端软件来实现异常管理平台。

 

[1] Base64是一种使用64基的位置计数法。它使用2的最大次方来代表仅可打印的ASCII 字符。这使它可用来作为电子邮件的传输编码。在Base64中的变量使用字符A-Z、a-z和0-9 ,这样共有62个字符,用来作为开始的64个数字,最后两个用来作为数字的符号在不同的系统中而不同。

posted on 2008-11-18 05:37  Gary Zhang  阅读(2343)  评论(2编辑  收藏  举报

导航