开发自定义累积函数体

使用自定义累积 functoid 对实例消息中多次发生的值执行累积操作。

在开发累积功能元件时,您必须实现三个函数。 这三个函数对应于映射执行累积所需的初始化、累积和获取操作。 在讨论这些函数之前,讨论线程安全性非常重要。

编写 Thread-Safe Functoid

functoid 代码必须是线程安全的,因为在压力条件下,映射的多个实例可能同时运行。 要记住的一些要点包括:

  • 静态状态必须是线程安全的。

  • 实例状态不一定需要是线程安全的。

  • 设计时要考虑在高压条件下运行。 尽可能避免使用锁。

  • 如果可能,请避免进行同步。

    BizTalk Server 提供了一种简单的机制,可降低编写线程安全累积功能单元的复杂性。 这三个函数具有相同的第一个参数,即整数索引值。 BizTalk Server 在调用初始化函数时为索引值分配唯一的数字。 可以将此值用作包含累积值的数组中的索引,如以下代码所示:

private HashTable cumulativeArray = new HashTable();  
…  
// Initialization function  
public string InitCumulativeMultiply(int index)  
{  
    cumulativeArray[index] = 1.0;  
    return string.Empty;  
}  

此示例使用 HashTable 而不是 ArrayList。 这是因为初始化函数可能不会使用顺序索引值调用。

实现三个累积函数

你需要为开发的每个自定义累积 functoid 实现三个函数。 下表汇总了必须在构造函数中调用的函数和方法来设置它们。 所有函数都返回字符串值。

注释

确定每个函数的最佳名称,但每个函数必须指定参数的数量和类型。

函数用途 论据 设置引用 设置内联脚本
初始化 int 索引 SetExternalFunctionName SetScriptBuffer with functionNumber = 0
累积 int 索引、字符串 val、字符串范围 SetExternalFunctionName2 SetScriptBuffer with functionNumber = 1
获取 int 索引 SetExternalFunctionName3 SetScriptBuffer with functionNumber = 2

初始化

初始化使你能够准备用于执行累积的机制。 可以初始化数组、重置一个或多个值或根据需要加载其他资源。 不使用字符串返回值。

累积

这是执行适合您功能单元的累积运算的位置。 BizTalk Server 传入以下三个参数:

  • 索引 表示映射实例的整数值。 可能有多个同时运行的映射实例。

  • 瓦尔。 包含应累积的值的字符串。 除非你正在编写字符串 Cumulate functoid,否则它是一个数值。

  • 范围。 一个字符串,其中包含一个数字,指示应累积哪些元素或属性值。 实际值由实现确定。

    决定要累积哪些值以及要忽略的值。 例如,可以忽略不低于 0 的值,但在值不是数值时引发异常。 BaseFunctoid 提供两个函数(IsDateIsNumeric),以帮助进行验证。

注释

如果在内联脚本中使用 IsDateIsNumeric ,请务必设置 RequiredGlobalHelperFunctions ,使函数可供脚本使用。

不使用字符串返回值。

获取

当 BizTalk Server 完成遍历地图中 functoid 设置确定的所有值时,它会请求累积的值。 get 函数有一个参数, Index即表示映射实例的整数值。 函数应使用索引值来查找并返回累积值作为字符串。

示例:

以下示例演示如何创建自定义 functoid 来执行累积乘法。 它依赖于包含三个字符串资源和 16x16 像素位图资源的资源文件。

using System;  
using Microsoft.BizTalk.BaseFunctoids;  
using System.Reflection;  
using System.Text;  
using System.Collections;  
using System.Globalization;  
  
namespace Microsoft.Samples.BizTalk.CustomFunctoid  
{  
    public class CumulativeMultiplyFunctoid : BaseFunctoid  
    {  
        private ArrayList myCumulativeArray = new ArrayList();  
  
        public CumulativeMultiplyFunctoid() : base()  
        {  
            //ID for this functoid  
            ID = 6001;  
  
            // Resource assembly must be ProjectName.ResourceName if building with VS.Net  
            SetupResourceAssembly("Microsoft.Samples.BizTalk.CustomFunctoid.CustomFunctoidResources", Assembly.GetExecutingAssembly());  
  
            // Pass the resource ID names for functoid name, tooltip  
            // description and the 16x16 bitmap for the Map palette  
            SetName("IDS_CUMULATIVEMULTIPLYFUNCTOID_NAME");  
            SetTooltip("IDS_CUMULATIVEMULTIPLYFUNCTOID_TOOLTIP");  
            SetDescription("IDS_CUMULATIVEMULTIPLYFUNCTOID_DESCRIPTION");  
            SetBitmap("IDB_CUMULATIVEMULTIPLYFUNCTOID_BITMAP");  
  
            // Put this string handling function under the Cumulative  
            // Functoid tab in the Visual Studio toolbox for functoids  
            Category = FunctoidCategory.Cumulative;  
  
            // 2 required parameters, no optional parameters  
            SetMinParams(1);  
            SetMaxParams(2);  
  
            // Functoid accepts three inputs  
            AddInputConnectionType(ConnectionType.AllExceptRecord);  
            AddInputConnectionType((~ConnectionType.FunctoidCount) & (~ConnectionType.FunctoidIndex) & (~ConnectionType.FunctoidIteration) & (~ConnectionType.FunctoidCumulative) & (~ConnectionType.FunctoidLooping) & (~ConnectionType.Record));  
            AddInputConnectionType(ConnectionType.AllExceptRecord);  
  
            // Set the output connection type  
            OutputConnectionType = ConnectionType.AllExceptRecord;  
  
            // Set the Initialize, Cumulative and Get functions  
            SetExternalFunctionName(GetType().Assembly.FullName, "Microsoft.Samples.BizTalk.CustomFunctoid.CumulativeMultiplyFunctoid", "InitCumulativeMultiply");  
            SetExternalFunctionName2("AddToCumulativeMultiply");  
            SetExternalFunctionName3("GetCumulativeMultiply");  
        }  
  
        // Initialization function  
        public string InitCumulativeMultiply(int index)  
        {  
            if (index >= 0)  
            {  
                if (index >= myCumulativeArray.Count)  
                {  
                    myCumulativeArray.Add(1.0);  
                }  
                else  
                {  
                    myCumulativeArray[index] = 1.0;  
                }  
            }  
  
            return "";  
        }  
  
        // Cumulative function  
        public string AddToCumulativeMultiply(int index, string val, string reserved)  
        {  
            if (index < 0 || index >= myCumulativeArray.Count)  
            {  
                return "";  
            }  
  
            if (IsNumeric(val))  
            {  
                double dval = Convert.ToDouble(val, CultureInfo.InvariantCulture);  
                myCumulativeArray[index] = (double)(myCumulativeArray[index]) * dval;  
            }  
            return myCumulativeArray[index].ToString();  
        }  
  
        // Get Function  
        public string GetCumulativeMultiply(int index)  
        {  
            if (index < 0 || index >= myCumulativeArray.Count)  
            {  
                return "";  
            }  
  
            return myCumulativeArray[index].ToString();  
        }  
    }  

另请参阅

使用 BaseFunctoid
开发自定义内联功能组件 (Functoid)
自定义 Functoid(BizTalk Server 示例)