[AX]AX2012 AIF(七):创建.NET程序集变换器

在增强型AIF端口中可以配置出入站变换器,它的作用是在消息处理过程中(http://msdn.microsoft.com/EN-US/library/gg731873.aspx)对消息做前后置处理,比如来源消息不是很规范,我们需要在正式处理前做数据的整理,这个就可以使用变换器来完成。顺便要说明的是,管道也是用来处理消息的,它与变换器是有区别的:管道可用于同步/异步数据交换,而变换器只能应用于异步交换;调用时机上入站消息在放入到消息队列前应用变换器,从入站消息队列取出消息正式处理前应用管道。

有两种类型的变换器可用,一是使用XSLT对XML格式的消息做处理变换,第二种则是我们使用.NET开发的程序集变换器,它可以处理任何自定义的格式,比如逗号分隔的CSV格式。一个.NET变换器实际上是在.NET的Class library工程中创建一个实现Microsoft.Dynamics.IntegrationFramework.Transform.ITransform接口的类,在目录C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin下可以找到需要引用的程序集Microsoft.Dynamics.IntegrationFramework.dll。

一个.net变换器类是这个样子的:

using System.Text;
using System.IO;
using System.Xml;
using Microsoft.Dynamics.IntegrationFramework.Transform;

namespace AssemblyTransformTest
{
    public class Class1 : ITransform
    {
        public void Transform(System.IO.Stream input, System.IO.Stream output, string config)
        {
            StreamReader sreader = new StreamReader(input);
            XmlTextWriter xwriter = new XmlTextWriter(output, Encoding.UTF8);
            string[] fieldArray;
            string[] dataArray;
            string rootName = "Customers";
            string rowName = "Customer";
            int i;
            
            // Get the element names from the headings
            string lineIn = sreader.ReadLine();
            fieldArray = lineIn.Split(',');

            // Start writing the XML file.
            xwriter.Formatting = Formatting.Indented;
            xwriter.WriteStartDocument();
            xwriter.WriteComment("customers.xml file");
            xwriter.WriteStartElement(rootName);

            lineIn = sreader.ReadLine();
            while (lineIn != null)
            {
                // Loop through each line of data in the file.
                xwriter.WriteStartElement(rowName); 
                dataArray = lineIn.Split(',');
                // Write each element.
                for (i = 0; i <= dataArray.GetUpperBound(0); i++)
                {
                    xwriter.WriteElementString(fieldArray[i], dataArray[i]);
                }
                // Write the </Customer> end element.
                xwriter.WriteEndElement();
                lineIn = sreader.ReadLine();
            }
            sreader.Close();
            // Write the </Customers> end element.
            xwriter.WriteEndElement();
            xwriter.Close();
        }
    }
}

它处理CSV输入格式的数据:

Num,AccountName,CustGroup,Currency
1,Fourth Coffee,20,USD
2,Graphic Design Institute,20,USD
3,Lucerne Publishing,20,EUR

返回XML格式的的结果:

<?xml version="1.0" encoding="utf-8"?>
<!--customers.xml file-->
<Customers>
  <Customer>
    <Num>1</Num>
    <AccountName>Fourth Coffee</AccountName>
    <CustGroup>20</CustGroup>
    <Currency>USD</Currency>
  </Customer>
  <Customer>
    <Num>2</Num>
    <AccountName>Graphic Design Institute</AccountName>
    <CustGroup>20</CustGroup>
    <Currency>USD</Currency>
  </Customer>
  <Customer>
    <Num>3</Num>
    <AccountName>Lucerne Publishing</AccountName>
    <CustGroup>20</CustGroup>
    <Currency>EUR</Currency>
  </Customer>
</Customers>

在AIF端口中使用这个.NET变换器前,我们还需要先将它导入到AX,导入工具在Tools > Application Integration Framework > Manage transforms窗口,在这里创建一个.net程序集类型的变换器,装载相应的程序集文件dll,选择要应用的变换器类,一个程序集是可以包含多个变换器类的。需要注意变换器程序集需要同时拷贝分发到服务器bin目录(C:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\Bin)和客户端bin目录(C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin)。

原文地址:https://www.cnblogs.com/duanshuiliu/p/2886318.html