我们知道,微软产品一般都提供三层API供程序员调用: 供C#/C++等高级语言调用的API,WMI和命令行。
在BizTalk Server 2006中,微软也提供了类似的三层API给我们使用,它们分别是: ExplorerOM, WMI和BtsTask.exe/BtsDeploy.exe。下面我们对如何使用三层不同的API对BizTalk Server 中的各类Artifacts进行操控作一个简要介绍。
一、利用 Microsoft.BizTalk.ExplorerOM;
1.引用Assembly: “c:\Program Files\Microsoft BizTalk Server 2006\Developer Tools\microsoft.biztalk.explorerom.dll”
2.using Microsoft.BizTalk.ExplorerOM;
3.例如创建一个ReceivePort的代码如下:
static void CreateReceivePort()
{
BtsCatalogExplorer root = new BtsCatalogExplorer();
try
{
root.ConnectionString = "Server=.;Initial Catalog=BizTalkMgmtDb;Integrated Security=SSPI;";
//Create a new one way receive port.
ReceivePort myreceivePort = root.AddNewReceivePort(false);
//Note that if you do not set the name property of the ReceivePort,
//it will use the default name generated.
myreceivePort.Name = "My Receive Port";
myreceivePort.Tracking = TrackingTypes.AfterReceivePipeline;
//Try to commit the changes made so far. If it fails, roll-back
//all the changes.
root.SaveChanges();
}
catch (Exception e)
{
root.DiscardChanges();
throw e;
}
}
二、WMI。利用WMI对BizTalk Server 2006的Artifacts进行操控:
1. 利用VBScript中的 GetObject和 CreateObject 对Artifacts进行操控:
下面举例Enlist 一个Orchestration:(EnlistOrch.vbs)
EnlistOrch
Sub EnlistOrch()
'Get the command line arguments entered for the script
Dim Args: Set Args = WScript.Arguments
'error handling is done by explicity checking the err object rather than using
'the VB ON ERROR construct, so set to resume next on error.
on error resume next
'Make sure the expected number of arguments were provided on the command line.
'if not, print usage text and exit.
If (Args.Count < 2) Or (Args.Count > 3) Then
PrintUsage()
wscript.quit 0
End If
Dim InstSet, Inst, Query, OrchestrationName, AssemblyName, HostName, Start
Dim AutoEnableReceiveLocation: AutoEnableReceiveLocation = 2
Dim AutoResumeOrchestrationInstance: AutoResumeOrchestrationInstance = 2
OrchestrationName = Args(0)
AssemblyName = Args(1)
'Check if orchestration is to be started
If (Args.Count = 3) Then
If ("Start" = Args(2)) Then
Start = True
Else
wscript.echo "Wrong optional flag."
PrintUsage()
wscript.quit 0
End If
End If
'set up a WMI query to acquire a list of defaul inprocess hosts
'This should be a list of zero or one host.
Query = "SELECT * FROM MSBTS_HostSetting WHERE IsDefault =""TRUE"""
Set InstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
'Check for error condition before continuing.
If Err <> 0 Then
PrintWMIErrorthenExit Err.Description, Err.Number
End If
'if Default Host found, get Host Name and NT Group Name. There is only one default host.
If InstSet.Count > 0 Then
For Each Inst In InstSet
HostName = Inst.Name
If Err <> 0 Then
PrintWMIErrorthenExit Err.Description, Err.Number
End If
wscript.echo "Using default inprocess host " & HostName & "."
Next
End If
'set up a WMI query to acquire a list of orchestrations with the given Name and
'AssemblyName key values. This should be a list of zero or one Orchestrations.
Query = "SELECT * FROM MSBTS_Orchestration WHERE Name =""" & OrchestrationName & """ AND AssemblyName = """ & AssemblyName & """"
