我们知道,微软产品一般都提供三层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 & """"
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 orchestration found, enlist the orchestration, otherwise print error and end.
If InstSet.Count > 0 then
For Each Inst in InstSet
Inst.Enlist(HostName)
If Err <> 0 Then
PrintWMIErrorThenExit Err.Description, Err.Number
End If
wscript.echo "The Orchestration was successfully enlisted."
If Start Then
Inst.Start AutoEnableReceiveLocation, AutoResumeOrchestrationInstance
If Err <> 0 Then
PrintWMIErrorThenExit Err.Description, Err.Number
End If
wscript.echo "The Orchestration was successfully started."
End If
Next
Else
wscript.echo "No orchestration was found matching that Name and AssemblyName."
End If
End Sub
2. 利用C#中的 System.Management命名空间对Artifacts进行操控:
a. 引用Assembly: “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll”
b. using System.Management;
c.例如Enlist 一个Orchestration的代码如下:
static private void EnlistOrch()
{
//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.
//Create the WMI search object.
ManagementObjectSearcher Searcher = new ManagementObjectSearcher();
Console.WriteLine("Got the args");
// create the scope node so we can set the WMI root node correctly.
ManagementScope Scope = new ManagementScope("root\\MicrosoftBizTalkServer");
Searcher.Scope = Scope;
string HostName = "";
// Build a Query to enumerate the MSBTS_Orchestration instances with the
// specified name and assembly name.
SelectQuery Query = new SelectQuery();
Query.QueryString = "SELECT * FROM MSBTS_HostSetting WHERE IsDefault ='TRUE'";
// Set the query for the searcher.
Searcher.Query = Query;
ManagementObjectCollection QueryCol = Searcher.Get();
foreach (ManagementObject Inst in QueryCol)
{
//Get the default host name.
HostName = Inst.Properties["Name"].Value.ToString();
Console.WriteLine("The Orchestration will be enlisted with the default host: " + HostName + ".");
}
// Build a Query to enumerate the MSBTS_Orchestration instances with the
// specified name and assembly name.
Query.QueryString = "SELECT * FROM MSBTS_Orchestration WHERE Name = '" + args[0]+ "' AND AssemblyName = '" + args[1] + "'";
// Set the query for the searcher.
Searcher.Query = Query;
QueryCol = Searcher.Get();
// Use a bool to tell if we enter the for loop
// below because Count property is not supported
bool OrchestrationFound = false;
foreach (ManagementObject Inst in QueryCol)
{
// There is at least one Orchestration
OrchestrationFound = true;
ManagementBaseObject inParams = Inst.GetMethodParameters("Enlist");
//Fill in input parameter values
inParams["HostName"] = HostName;
//Execute the method
ManagementBaseObject outParams = Inst.InvokeMethod("Enlist",inParams,null);
Console.WriteLine("The Orchestration was successfully enlisted.");
}
if(!OrchestrationFound)
{
Console.WriteLine("No Orchestration found matching that Name and AssemblyName.");
}
}
三、BtsTask.exe / Btsdeploy.exe
Btsdeploy.exe 是BizTalk Server 2004保留下来的命令行工具,在2006中已经被BtsTask.exe替代了。
具体使用运行BtsTask.exe /? 参考帮助就可以了。
Microsoft (R) BizTalk Application Deployment Utility Version 3.5.1602.0
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
BTSTask.exe: Performs BizTalk application deployment tasks.
Usage:
BTSTask.exe <Command> [[<Parameter>] ...]
Commands:
ListApps Lists the BizTalk applications in the configuration
database.
AddApp Adds a new BizTalk application to the configuration
database.
ImportApp Imports a Windows Installer package into a BizTalk
application in the configuration database.
ExportApp Exports a BizTalk application in the configuration
database to a Windows Installer package.
RemoveApp Removes a BizTalk application from the configuration
database.
UninstallApp Uninstalls the given application and removes from
Add/Remove programs.
ListApp Lists the resources contained in a BizTalk application.
ListPackage Lists the resources contained in a BizTalk Windows
Installer package.
ListTypes Lists the supported resource types.
AddResource Adds a resource to a BizTalk application.
RemoveResource Removes a resource from a BizTalk application.
ImportBindings Imports an XML bindings file into a BizTalk application
in the configuration database.
ExportBindings Exports application bindings from configuration database
to an XML bindings file.
Example:
BTSTask.exe <Command> [[-Name:Value] ...]
For help on command: BTSTask.exe <Command> /?
Notes:
Parameter names are not case-sensitive and may be abbreviated.