none
Excel Reading Custom Pipeline for BizTalk Server RRS feed

  • 问题

  • 各位论坛的朋友,我在国外的一个网站看到了一个自定义Pipeline的文章,后来我下载了源代码,由于本人之前是从事Java编程方面的,有很多C#代码都不知道是什么含义,希望知道的朋友帮忙解释一下。

    还有一个疑问就是,我定义一个组件的时候,我想知道找个组件中主要方法的执行时机是什么?

    有两个问题:

    1.我不知道哪些是主要方法

    2.我不知道执行时机是什么

     

     

     

     

    namespace ExcelCustomePipeline
    {
        using System;
        using System.Resources;
        using System.Drawing;
        using System.Collections;
        using System.Reflection;
        using System.ComponentModel;
        using System.Text;
        using System.IO;
        using Microsoft.BizTalk.Message.Interop;
        using Microsoft.BizTalk.Component.Interop;
        using System.Xml;
        using System.Configuration;
        /// <summary>
        /// Implements custom pipeline component to append and/or prepend data to a stream.
        /// </summary>
        /// <remarks>
        /// MessageHandler class implements pipeline component that can be used in receive and
        /// send BizTalk pipelines. The pipeline component gets a data stream, appends
        /// and/or prepends user specified data to it and outputs modified stream.
        ///</remarks>
        [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
        [ComponentCategory(CategoryTypes.CATID_Any)]
        [ComponentCategory(CategoryTypes.CATID_Validate)]
        [System.Runtime.InteropServices.Guid("48BEC85A-20EE-40ad-BFD0-319B59A0DDBC")]
        public class MessageHandler :
     BaseCustomTypeDescriptor,
            IBaseComponent,
            Microsoft.BizTalk.Component.Interop.IComponent,
            Microsoft.BizTalk.Component.Interop.IPersistPropertyBag,
            IComponentUI
        {
      
            private string prependData  = null;
            private string appendData   = null;

            static ResourceManager resManager = new ResourceManager("ExcelFramework.Custompipeline.Msg", Assembly.GetExecutingAssembly());

     /// <summary>
     /// Constructor initializes base class to allow custom names and description for component properies
     /// </summary>
            public MessageHandler()
                :
      base(resManager)
     {
     }
            /// <summary>
            /// Data to prepend at the beginning of a stream.
            /// </summary> 
     [
     MsgPropertyName("PropPrependData"),
     MsgDescription("DescrPrependData")
     ]
            public string PrependData
            {
                get {   return prependData; }
                set {   prependData = value;}
            }
            /// <summary>
            /// Data to append at the end of stream.
            /// </summary>
            [
     MsgPropertyName("PropAppendData"),
     MsgDescription("DescrAppendData")
     ]
            public string AppendData
            {
                get {   return appendData;  }
                set {   appendData = value; }
            }

            /// <summary>
            /// Converts a string to its byte representation.
            /// </summary>
            /// <param name="str">String to be converted to byte representation.</param>
            /// <returns>Array of bytes that represents the string.</returns>
            private byte[] ConvertToBytes(string str)
            {
                byte[] data = null;

                if (str != null)
                {
                    data = UTF8Encoding.UTF8.GetBytes(str);
                }

                return data;
            }

            #region IBaseComponent
           
            /// <summary>
            /// Name of the component.
            /// </summary>
            [Browsable(false)]
            public string Name
            {
                get { return "ExcelCustomePipeline"; }
            }
           
            /// <summary>
            /// Version of the component.
            /// </summary>
            [Browsable(false)]
            public string Version
            {
                get {   return "1.0";   }
            }
           
            /// <summary>
            /// Description of the component.
            /// </summary>
            [Browsable(false)]
            public string Description
            {
                get { return "ExcelCustomePipeline"; }
            }
       
            #endregion
           
            #region IComponent

            /// <summary>
            /// Implements IComponent.Execute method.
            /// </summary>
            /// <param name="pc">Pipeline context</param>
            /// <param name="inmsg">Input message.</param>
            /// <returns>Processed input message with appended or prepended data.</returns>
            /// <remarks>
            /// IComponent.Execute method is used to initiate
            /// the processing of the message in pipeline component.
            /// </remarks>
            public IBaseMessage Execute(IPipelineContext pc, IBaseMessage inmsg)
            {
                IBaseMessagePart bodyPart = inmsg.BodyPart;
                IBaseMessageContext context = inmsg.Context;
                string Originalfilename = "";
                string OriginalfilePath = "";
                string Exceptionsfilename = "";
                string ExceptionsfilePath = "";
                Originalfilename = context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();
                Originalfilename = Originalfilename.Substring(Originalfilename.LastIndexOf("\\") + 1);
                Exceptionsfilename = Originalfilename;
                OriginalfilePath = @"C:\temp.xls";//BusinessLayer.ConstructMsg.GetFileName(1, Originalfilename);  //Get the Original file path to save
                ExceptionsfilePath =@"C:\Error.xls"; //BusinessLayer.ConstructMsg.GetFileName(4, Exceptionsfilename);  //Get the Exception file path to save
                if (bodyPart!=null)
                {
                    byte[] prependByteData = ConvertToBytes(prependData);
                    byte[] appendByteData = ConvertToBytes(appendData);
                    Stream originalStrm = bodyPart.GetOriginalDataStream();
                    string Archivepath = string.Empty;
                   
                    if (originalStrm != null)
                    {
                        MemoryStream memmorystream = new MemoryStream();
                        XmlDocument xmldocument = new XmlDocument();
                        Archivepath = SaveStreamToFile(originalStrm, OriginalfilePath); //Save file in temp directory
                        bodyPart.Data = memmorystream;
                    }

                }
                return inmsg;
            }
            #endregion

            #region SaveStreamToFile

           
            public string SaveStreamToFile(Stream inMsgStream, string filename)
            {

                int bufferSize = 4096;

                byte[] buffer = new byte[4096];

                int numBytesRead = 0;
                //string tempPath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
                FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);

                // Setup the stream writter and reader

                BinaryWriter w = new BinaryWriter(fileStream);

                w.BaseStream.Seek(0, SeekOrigin.End);

                if (inMsgStream != null)
                {

                    inMsgStream.Seek(0, SeekOrigin.Begin);

                    // Copy the data from the msg to the file

                    int n = 0;

                    do
                    {
                        n = inMsgStream.Read(buffer, 0, bufferSize);

                        if (n == 0) // We're at EOF

                            break;

                        w.Write(buffer, 0, n);

                        numBytesRead += n;

                    } while (n > 0);

                }

                w.Flush();

                w.Close();
                return filename;
            }

            #endregion

            #region IPersistPropertyBag

            /// <summary>
            /// Gets class ID of component for usage from unmanaged code.
            /// </summary>
            /// <param name="classid">Class ID of the component.</param>
            public void GetClassID(out Guid classid)
            {
                classid = new System.Guid("48BEC85A-20EE-40ad-BFD0-319B59A0DDBC");
            }
           
            /// <summary>
            /// Not implemented.
            /// </summary>
            public void InitNew()
            {
            }
           
            /// <summary>
            /// Loads configuration property for component.
            /// </summary>
            /// <param name="pb">Configuration property bag.</param>
            /// <param name="errlog">Error status (not used in this code).</param>
            public void Load(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, Int32 errlog)
            {
                string val = (string)ReadPropertyBag(pb, "AppendData");
                if (val != null) appendData = val;

                val = (string)ReadPropertyBag(pb, "PrependData");
                if (val != null) prependData = val;
            }
           
            /// <summary>
            /// Saves the current component configuration into the property bag.
            /// </summary>
            /// <param name="pb">Configuration property bag.</param>
            /// <param name="fClearDirty">Not used.</param>
            /// <param name="fSaveAllProperties">Not used.</param>
            public void Save(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, Boolean fClearDirty, Boolean fSaveAllProperties)
            {
                object val = (object)appendData;
                WritePropertyBag(pb, "AppendData", val);
               
                val = (object)prependData;
                WritePropertyBag(pb, "PrependData", val);
            }

            /// <summary>
            /// Reads property value from property bag.
            /// </summary>
            /// <param name="pb">Property bag.</param>
            /// <param name="propName">Name of property.</param>
            /// <returns>Value of the property.</returns>
            private static object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName)
            {
                object val = null;
                try
                {
                    pb.Read(propName,out val,0);
                }

                catch(ArgumentException)
                {
                    return val;
                }
                catch(Exception ex)
                {
                    throw new ApplicationException( ex.Message);
                }
                return val;
            }

            /// <summary>
            /// Writes property values into a property bag.
            /// </summary>
            /// <param name="pb">Property bag.</param>
            /// <param name="propName">Name of property.</param>
            /// <param name="val">Value of property.</param>
            private static void WritePropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName, object val)
            {
                try
                {
                    pb.Write(propName, ref val);
                }
                catch(Exception ex)
                {
                    throw new ApplicationException( ex.Message);
                }
            }
        

     

            #endregion

            #region IComponentUI

            /// <summary>
            /// Component icon to use in BizTalk Editor.
            /// </summary>
            [Browsable(false)]
            public IntPtr Icon
            {
      get
      {
       return ((Bitmap)resManager.GetObject("FixMsgBitmap")).GetHicon();
      }

            }

            /// <summary>
            /// The Validate method is called by the BizTalk Editor during the build
            /// of a BizTalk project.
            /// </summary>
            /// <param name="obj">Project system.</param>
            /// <returns>
            /// A list of error and/or warning messages encounter during validation
            /// of this component.
            /// </returns>
            public IEnumerator Validate(object obj)
            {
      IEnumerator enumerator = null;
      ArrayList   strList  = new ArrayList();

      // Validate prepend data property
      if ((prependData != null) &&
      (prependData.Length >= 64))
      {
       strList.Add(resManager.GetString("ErrorPrependDataTooLong"));
      }

      // validate append data property
      if ((appendData != null) &&
      (appendData.Length >= 64))
      {
       strList.Add(resManager.GetString("ErrorAppendDataTooLong"));
      }

      if (strList.Count > 0)
      {
       enumerator = strList.GetEnumerator();
      }

      return enumerator;
            }

            #endregion
        }
    }

     

    2010年5月24日 6:18

全部回复

  • 不知道他们是什么含义:

        [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
        [ComponentCategory(CategoryTypes.CATID_Any)]
        [ComponentCategory(CategoryTypes.CATID_Validate)]
        [System.Runtime.InteropServices.Guid("48BEC85A-20EE-40ad-BFD0-319B59A0DDBC")]

    不知道是什么含义:

     [
     MsgPropertyName("PropPrependData"),
     MsgDescription("DescrPrependData")
     ]

    不知道是什么含义:

    [Browsable(false)]

     

    2010年5月24日 6:34
  • 各位论坛的朋友,我在国外的一个网站看到了一个自定义Pipeline的文章,后来我下载了源代码,由于本人之前是从事Java编程方面的,有很多C#代码都不知道是什么含义,希望知道的朋友帮忙解释一下。

    [Forrest] 你可以参考一下Net4.0 C# 规范。在微软网站上有下载

    还有一个疑问就是,我定义一个组件的时候,我想知道找个组件中主要方法的执行时机是什么?

    有两个问题:

    1.我不知道哪些是主要方法

    [Forrest] 最主要的方法是Execute,这是一个必须重载的方法。

    2.我不知道执行时机是什么

    [Forrest]消息在pipeline接受的时候会触发execute方法。


    Thanks/Forrest Chen
    2010年8月13日 7:48