可以使用biztalk提供的ExplorerOM名称空间下的API对biztalk的各个组件进行管理,做Biztalk Administration Console做的那些管理任务,比如查看biztalk服务器上有多少个application,某个Application中有多少接收端口,有多少发送端口,增加删除Receive Location,增加删除发送端口等等。
BtsCatalogExplorer是ExplorerOM中的核心类,biztalk中的所有组件都通过BtsCatalogExplorer类对外暴露。
BtsCatalogExplorer的各种操作实际上是对biztalk的BizTalkMgmtDb数据库的操作,因为biztalk的静态信息,包括这个biztalk服务上有什么Application,接收端口,发送端口,有什么适配器,handler等等都保存在BizTalkMgmtDb数据库。
如果要对biztalk中的某个组件进行操作,必须先初始化一个BtsCatalogExplorer对象,并将需要操作的那个biztalk服务器相应的BizTalkMgmtDb数据库的连接字符串赋给BtsCatalogExplorer的ConnectionString属性。
BtsCatalogExplorer对biztalk组件的修改都是在内存中进行,只有当执行到SaveChanges方法时,才会把所有的修改一次性提交到BizTalkMgmtDb数据库做实际的修改。
这里以实例说明使用ExplorerOM对象模型新建receive location过程。
代码下载:CreateReceiveLocation.rar
一、  新建File adapter的Receive Location
 用下面代码在“My Receive Port”接收端口下新建一个name为“Receive Location6 - Code Created user”的File类型的Receive Location。
BtsCatalogExplorer root = new BtsCatalogExplorer();
try
{
    root.ConnectionString = "Data Source=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";
    
    ReceivePort rp = root.ReceivePorts["My Receive Port"];
    ReceiveLocation rl = rp.AddNewReceiveLocation();
rl.Name = "Receive Location6 - Code Created user";
rl.Address = "c:\\temp\\te*.xml";
    foreach (ReceiveHandler handler in root.ReceiveHandlers)
    {
        if (handler.TransportType.Name == "FILE" && handler.Name == "BizTalkServerApplication")
        {
            rl.ReceiveHandler = handler;
            break;
        }
    }
    Microsoft.BizTalk.ExplorerOM.ProtocolType protocol = root.ProtocolTypes["FILE"];
    rl.TransportType = protocol;
rl.ReceivePipeline = root.Pipelines[typeof(PassThruReceive).FullName, typeof(PassThruReceive).Assembly.FullName];
string proStr = "<CustomProps><RemoveReceivedFileRetryCount vt=\"19\">51</RemoveReceivedFileRetryCount><RemoveReceivedFileMaxInterval vt=\"19\">300001</RemoveReceivedFileMaxInterval><FilePath vt=\"8\">F:\\temp</FilePath><FileMask vt=\"8\">pe*.xml</FileMask><Username vt=\"8\">a</Username><BatchSizeInBytes vt=\"19\">102401</BatchSizeInBytes><PollingInterval vt=\"19\">60001</PollingInterval><BatchSize vt=\"19\">21</BatchSize><Password vt=\"1\">a</Password><FileNetFailRetryInt vt=\"19\">51</FileNetFailRetryInt><RemoveReceivedFileDelay vt=\"19\">11</RemoveReceivedFileDelay><RenameReceivedFiles vt=\"11\">0</RenameReceivedFiles><FileNetFailRetryCount vt=\"19\">51</FileNetFailRetryCount></CustomProps>";
    rl.TransportTypeData = proStr;
    rl.FragmentMessages = Fragmentation.Yes;
    rl.ServiceWindowEnabled = false;
    rl.Enable = true;
    root.SaveChanges();
}
catch (Exception e)
{
    root.DiscardChanges();
    throw e;
}
需要注意的是,ReceiveLocation的TransportTypeData属性。不同的适配器有不同的属性集,biztalk通过这个TransportTypeData属性接收这些不同的属性,属性的数据类型是string,实际上是name/value对组成的xml形式的属性集合。类似这样的形式,包括了File adapter所有的定制属性了:
                      
<CustomProps>
  <RemoveReceivedFileRetryCount vt="19">51</RemoveReceivedFileRetryCount> 
  <RemoveReceivedFileMaxInterval vt="19">300001</RemoveReceivedFileMaxInterval> 
  <FilePath vt="8">F:\temp</FilePath>
  <FileMask vt="8">te*.xml</FileMask> 
  <Username vt="8">a</Username> 
  <BatchSizeInBytes vt="19">102401</BatchSizeInBytes> 
  <PollingInterval vt="19">60001</PollingInterval> 
  <BatchSize vt="19">21</BatchSize> 
  <Password vt="1" >password</Password>
  <FileNetFailRetryInt vt="19">51</FileNetFailRetryInt> 
  <RemoveReceivedFileDelay vt="19">11</RemoveReceivedFileDelay> 
  <RenameReceivedFiles vt="11">0</RenameReceivedFiles> 
  <FileNetFailRetryCount vt="19">51</FileNetFailRetryCount> 
</CustomProps>
ReceiveLocation.Address属性很重要,这个属性就是biztalk Administration Console中Receive Location列表中,Receive Location的URI属性,指示这个Location接收消息的位置,这个URI在一个biztalk server上所有Receive Location中必须唯一。
对于File适配器,ReceiveLocation.Address属性由File adater的FilePath和Filemask属性组合而成。在新建File的Receive Location时,必须设置ReceiveLocation.Address,如果同时在TransportTypeData设置了FilePath和Filemask属性,ReceiveLocation.Address会覆盖FilePath和Filemask属性的设置,所以,没必要在TransportTypeData中设置FilePath和Filemask属性。
一、  新建FTP adapter的Receive Location
 同样可以在代码中使用ExplorerOM新建一个使用FTP适配器的Receive Location,下面是代码,跟建立File的Location稍有不同:
BtsCatalogExplorer root = new BtsCatalogExplorer();
try
{
    root.ConnectionString = "Data Source=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";
    string proStr = "<Config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><serverAddress>chnking1.com</serverAddress><serverPort>21</serverPort><userName>kent</userName><password /><fileMask>1*.xml</fileMask><targetFolder>testFolder1</targetFolder><representationType>binary</representationType><maximumBatchSize>0</maximumBatchSize><maximumNumberOfFiles>0</maximumNumberOfFiles><passiveMode>False</passiveMode><firewallType>NoFirewall</firewallType><firewallPort>21</firewallPort><pollingUnitOfMeasure>Seconds</pollingUnitOfMeasure><pollingInterval>60</pollingInterval><errorThreshold>10</errorThreshold><maxFileSize>100</maxFileSize><uri>ftp://chnking.com:21/testFolder/*.xml</uri></Config>";
    proStr = CreateLocationTransportData(proStr);
    ReceivePort rp = root.ReceivePorts["My Receive Port"];
    ReceiveLocation rl = rp.AddNewReceiveLocation();
    rl.Name = "Receive Location FTP - Code Created";
    rl.Address = "ftp://chnking.com:21/testFolder/t*.xml";
    foreach (ReceiveHandler handler in root.ReceiveHandlers)
    {
        if (handler.TransportType.Name == "FTP" && handler.Name == "BizTalkServerApplication")
        {
            rl.ReceiveHandler = handler;
            break;
        }
    }
    Microsoft.BizTalk.ExplorerOM.ProtocolType protocol = root.ProtocolTypes["FTP"];
    rl.TransportType = protocol;
    rl.ReceivePipeline = root.Pipelines[typeof(PassThruReceive).FullName, typeof(PassThruReceive).Assembly.FullName];
    rl.TransportTypeData = proStr;
    rl.FragmentMessages = Fragmentation.Yes;
    rl.ServiceWindowEnabled = false;
    rl.Enable = true;
    root.SaveChanges();
}
catch (Exception e)
{
    root.DiscardChanges();
    throw e;
}
FTP适配器具有的定制属性有下面这些:
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <serverAddress>chnking.com</serverAddress> 
  <serverPort>21</serverPort> 
  <userName>kent</userName> 
  <password /> 
  <fileMask>*.xml</fileMask> 
  <targetFolder>testFolder</targetFolder> 
  <representationType>binary</representationType> 
  <maximumBatchSize>0</maximumBatchSize> 
  <maximumNumberOfFiles>0</maximumNumberOfFiles> 
  <passiveMode>False</passiveMode> 
  <firewallType>NoFirewall</firewallType> 
  <firewallPort>21</firewallPort> 
  <pollingUnitOfMeasure>Seconds</pollingUnitOfMeasure> 
  <pollingInterval>60</pollingInterval> 
  <errorThreshold>10</errorThreshold> 
  <maxFileSize>100</maxFileSize> 
</Config>
但是跟File适配器不同,FTP适配器需要的TransportTypeData比较特别,需要下面的形式:
需要把前面<Config>节部分的内容转成字符串格式,然后嵌入到<CustomProps>的<AdapterConfig>节点下,代码中proStr = CreateLocationTransportData(proStr);就是用来做这个转换的:
private static string CreateLocationTransportData(string configString)
{
    XmlDocument xd = new XmlDocument();
    XmlElement xeRoot = xd.CreateElement("CustomProps");
    xd.AppendChild(xeRoot);
    XmlElement xeConfig = xd.CreateElement("AdapterConfig");
    xeRoot.AppendChild(xeConfig);
    XmlAttribute xaVT = xd.CreateAttribute("vt");
    xeConfig.Attributes.Append(xaVT);
    xaVT.Value = "8";
    XmlDocument myXmlDocument = new XmlDocument();
    myXmlDocument.LoadXml(configString);
    xeConfig.InnerText = myXmlDocument.DocumentElement.OuterXml;
    return xd.InnerXml;
}
处理过的TransportTypeData数据是这样的:
<CustomProps>
  <AdapterConfig vt="8"><Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><serverAddress>chnking1.com</serverAddress><serverPort>21</serverPort><userName>kent</userName><password /><fileMask>1*.xml</fileMask><targetFolder>testFolder1</targetFolder><representationType>binary</representationType><maximumBatchSize>0</maximumBatchSize><maximumNumberOfFiles>0</maximumNumberOfFiles><passiveMode>False</passiveMode><firewallType>NoFirewall</firewallType><firewallPort>21</firewallPort><pollingUnitOfMeasure>Seconds</pollingUnitOfMeasure><pollingInterval>60</pollingInterval><errorThreshold>10</errorThreshold><maxFileSize>100</maxFileSize></Config>
  </AdapterConfig>
</CustomProps>
对于FTP适配器,ReceiveLocation.Address属性由FTP adater的serverAddress、serverPort、targetFolder和fileMask属性组合而成,但是在新建FTP Receive Location的代码中ReceiveLocation.Address和TransportTypeData的serverAddress等属性也都需要设置,所以请保证两边设置的值是一致的。
 
                    
                     
                    
                 
                    
                 

 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号