在 Dynamics AX 2012 中直接从 X++ 调用 AIF 文档服务
已出版: 2025年2月16日 UTC 11:23:22
在本文中,我解释了如何直接从 X++ 代码调用 Dynamics AX 2012 中的应用程序集成框架文档服务,模拟入站和出站调用,这可以更轻松地查找和调试 AIF 代码中的错误。
Calling AIF Document Services Directly from X++ in Dynamics AX 2012
本文中的信息基于 Dynamics AX 2012 R3。它可能对其他版本有效,也可能无效。
我最近正在帮助一位客户实施应用集成框架 (AIF) 入站端口,以便根据从其他系统收到的数据创建客户。由于 Dynamics AX 已经提供了 CustCustomer 文档服务,该服务实现了此逻辑,因此我们决定保持简单并使用标准解决方案。
然而,很快发现,让外部系统生成 Dynamics AX 可以接受的 XML 存在很多问题。Dynamics AX 生成的 XML 模式非常复杂,而且 Dynamics AX 中似乎存在一些错误,有时会导致它拒绝根据其他工具模式有效的 XML,所以总的来说,事实证明它没有我想象的那么简单。
在此过程中,我经常费力地找出某些 XML 文件到底出了什么问题,因为 AIF 提供的错误消息信息量太小。这也很乏味,因为我必须等待外部系统通过 MSMQ 发送新消息,然后等待 AIF 再次接收并处理该消息,然后才能看到错误。
因此,我调查了是否可以使用本地 XML 文件直接调用服务代码以进行更快速的测试,事实证明是可以的 - 不仅如此,它真的很简单,并且实际上提供了更多有意义的错误消息。
下面的示例作业读取本地 XML 文件并尝试将其与 AxdCustomer 类(CustCustomer 服务使用的文档类)一起使用以创建客户。如果需要,您可以为所有其他文档类(例如 AxdSalesOrder)创建类似的作业。
{
FileNameOpen fileName = @'C:\\TestCustomerCreate.xml';
AxdCustomer customer;
AifEntityKey key;
#File
;
new FileIoPermission(fileName, #IO_Read).assert();
customer = new AxdCustomer();
key = customer.create( XmlDocument::newFile(fileName).xml(),
new AifEndpointActionPolicyInfo(),
new AifConstraintList());
CodeAccessPermission::revertAssert();
info('Done');
}
customer.create() 方法 (对应于 AIF 中的“创建”服务操作) 返回的 AifEntityKey 对象包含有关创建了哪个客户的信息,其中包括创建的 CustTable 记录的 RecId。
如果您尝试测试的是出站端口,或者您只是需要一个 XML 在入站端口上的示例,那么您也可以使用文档类将客户导出到文件,而是通过调用 read() 方法(对应于“读取”服务操作),如下所示:
{
FileNameSave fileName = @'C:\\TestCustomerRead.xml';
Map map = new Map( Types::Integer,
Types::Container);
AxdCustomer customer;
AifEntityKey key;
XMLDocument xmlDoc;
XML xml;
AifPropertyBag bag;
#File
;
map.insert(fieldNum(CustTable, AccountNum), ['123456']);
key = new AifEntityKey();
key.parmTableId(tableNum(CustTable));
key.parmKeyDataMap(map);
customer = new AxdCustomer();
xml = customer.read(key,
null,
new AifEndpointActionPolicyInfo(),
new AifConstraintList(),
bag);
new FileIoPermission(fileName, #IO_Write).assert();
xmlDoc = XmlDocument::newXml(xml);
xmlDoc.save(fileName);
CodeAccessPermission::revertAssert();
info('Done');
}
当然,您应该将“123456”替换为您想要读取的客户的帐号。