以编程方式创建 MSMQ 接收位置和发送端口

本主题介绍如何使用 WMI 为 MSMQ 适配器创建端口或位置。

有关详细信息,请参阅 UI 指南和开发人员 API 命名空间参考中的“使用 WMI 创建包含日期时间计划配置的接收位置”。

设置属性值

创建端口或位置的过程始终相同:

  1. 创建正确类型的对象。

  2. 设置对象的属性值。

  3. 将对象值提交到数据库。

    所有适配器都具有某些属性,例如 HostName。 通过直接将它们分配给对象来设置这些常见属性。 以下 C# 代码显示了一个典型情况:

objReceiveLocation["HostName"] = "BizTalkServerApplication";  

将值分配给并非所有适配器共享的属性。 在字符串中创建 XML 文档,并将该字符串分配给 CustomCfg 属性。 以下 C# 代码显示了文件适配器的典型情况:

objReceiveLocation["CustomCfg"] =   
        @"<CustomProps>"  
        + @"<BatchSize>20</BatchSize>"  
        + @"<FileMask>*.xml</FileMask>"  
        + @"<FileNetFailRetryCount>5</FileNetFailRetryCount>"  
        + @"<FileNetFailRetryInt>5</FileNetFailRetryInt>"  
        + @"</CustomProps>";  

CustomProps 元素中标记的名称是适配器用于属性的内部名称。

MSMQ 适配器在 CustomProps 标记内具有单个标记 AdapterConfig。 AdapterConfig 标记包含一个 XML 标记字符串,用于包含在 Config 标记中的自定义属性值。 但是,标记已编码:“<”替换“<”,而“>”替换“>”。 例如,MSMQ 属性的适配器子集的 XML 可能如下所示:

<Config>  
    <batchSize>40</batchSize>  
</Config>  

请注意,不使用 vt 属性。 分配给 CustomCfg 属性的字符串在编码后如下所示:

<CustomProps><AdapterConfig vt="8"><Config><batchSize>40</batchSize></Config></AdapterConfig></CustomProps>  

自定义属性名称

下表描述了 MSMQ 适配器 发送 自定义属性的内部名称。

发送自定义属性名称 显示名称
确认类型 确认类型
管理队列 管理队列
证书 证书指纹
加密算法 加密算法
最大消息大小 最大消息大小(以 KB 为单位)
密码 密码
优先级 消息优先级
队列 目标队列
追 讨 可恢复
分段支持 支持分段
sendBatchSize(发送批量大小) 批大小
sendQueueName 目的地队列
超时 超时
timeOutUnits 超时单位
事务 事务性
使用认证 (useAuthentication) 使用身份验证
使用死信队列 使用死信队列
useJournalQueue 使用日记队列
用户名 用户名

下表描述了 MSMQ 适配器 接收 自定义属性的内部名称。

接收自定义属性名称 显示名称
批处理大小 批大小
密码 密码
队列 队列
串行处理 串行处理
事务性 事务性
用户名 用户名

示例代码

以下 C# 程序为 MSMQ 适配器创建单个接收位置。 它假定接收端口 ReceivePort1 存在并使用帮助程序函数对自定义属性进行编码和格式化。

using System;  
using System.Management;  
using System.Text;  
  
namespace CreateReceive  
{  
    ///   
    /// Program to create a receive ___location.  
    ///   
    class CreateReceive  
    {  
        /// The main entry point for the application.  
  
        [STAThread]  
        static void Main()  
        {  
            // Custom properties & values  
            string cfg =   
                    @"<queue>FORMATNAME:DIRECT=OS:MYMACHINE02\PRIVATE$\QUEUE2</queue>"  
                    + @"<batchSize>40</batchSize>"  
                    + @"<transactional>true</transactional>"  
                    + @"<serialProcessing>false</serialProcessing>";  
  
            CreateReceiveLocation (  
                    "Code Created Location",  
                    "ReceivePort1",  
                    "MSMQ",  
                    "BizTalkServerApplication",  
                    EncodeCustomProps(cfg),  // Encode the custom props  
                    "Microsoft.BizTalk.DefaultPipelines.PassThruReceive,"   
                            + " Microsoft.BizTalk.DefaultPipelines,"   
                            + " Version=3.0.1.0, Culture=neutral,"   
                            + " PublicKeyToken=31bf3856ad364e35",  
                    @"FORMATNAME:DIRECT=OS:MYMACHINE\PRIVATE$\QUEUE2"  
                );  
  
        }  
  
        static string EncodeCustomProps (string cp) {  
  
            // Enclose the properties and values in a Config> tag.  
            StringBuilder tmp = new StringBuilder( @"<Config>" + cp + @"</Config>");  
  
            // Encode the string  
            tmp = tmp.Replace("<","<");  
            tmp = tmp.Replace(">",">");  
  
            return (  
                // Enclose the encoded string with necessary tags  
                "<CustomProps><AdapterConfig vt=\"8\">"  
                + tmp.ToString()   
                + "</AdapterConfig></CustomProps>");  
  
        }  
  
        static void CreateReceiveLocation (  
            string name,            // Location name  
            string port,            // Receive port, already exists  
            string adapterName,     // The transport type  
            string hostName,        // BTS host  
            string customCfg,       // Encoded custom properties  
            string pipeline,        // Full specification of pipeline  
            string inboundTransport)// Inbound transport url  
        {  
            try   
            {  
                // Create options to store options in management object  
                PutOptions options = new PutOptions();  
                options.Type = PutType.CreateOnly;  
  
                // Create a management object  
                // Get the class  
                ManagementClass objReceiveLocationClass =   
                           new ManagementClass(  
                               "root\\MicrosoftBizTalkServer",   
                               "MSBTS_ReceiveLocation",   
                               null);  
                // Create an instance of the member of the class  
                ManagementObject objReceiveLocation =  
                          objReceiveLocationClass.CreateInstance();  
  
                // Fill in the properties  
                objReceiveLocation["Name"] = name;  
                objReceiveLocation["ReceivePortName"] = port;  
                objReceiveLocation["AdapterName"] = adapterName;  
                objReceiveLocation["HostName"] = hostName;  
                objReceiveLocation["PipelineName"] = pipeline;  
                objReceiveLocation["CustomCfg"] = customCfg;  
                objReceiveLocation["IsDisabled"] = true;  
                objReceiveLocation["InBoundTransportURL"] = inboundTransport;  
  
                // Put the options -- creates the receive ___location  
                objReceiveLocation.Put(options);  
            }  
            catch (Exception excep)  
            {  
                System.Console.WriteLine("Create Receive Location ({0}) failed - {1}",  
                                                name, excep.Message);  
            }  
        }  
    }  
}