PartnerManagement (BizTalk Server 範例)
PartnerManagement 範例示範如何使用ExplorerOM管理物件來管理BizTalk Server環境中的合作物件。
必要條件
您必須擁有BizTalk Server系統管理許可權,才能使用此範例中的系統管理物件。
Windows PowerShell腳本需要Windows PowerShell執行原則,才能允許腳本執行。 如需詳細資訊,請參閱 檢查執行原則。
此範例的用途
此範例示範如何使用 來自 Microsoft.BizTalk.ExplorerOM 命名空間的系統管理類別來管理合作物件。 合作物件代表商務程式可以與其互動的交易夥伴或後端應用程式。 如需一般合作物件的詳細資訊,請參閱 合作物件 或 角色連結和服務連結角色的檔。 此範例是以 Microsoft Visual C# 撰寫。 本主題也包含Windows PowerShell範例腳本。 此範例示範下列作業:
使用自訂或標準別名建立新合作物件,並將現有的BizTalk Server傳送埠至合作物件。
在 BizTalk 伺服器上加入具有現有角色連結的新合作物件。
取消登錄新合作物件。
刪除新合作物件。
此範例的位置
這個範例位於下列 SDK 位置:
<範例路徑>\管理員\ExplorerOM\PartnerManagement
下表顯示此範例中的檔案,並描述其用途。
檔案 | Description |
---|---|
PartnerManagement.cs | 此範例中示範之作業的 Visual C# 原始程式檔。 |
PartnerManagement.sln 和 PartnerManagement.csproj | 此範例的方案和專案檔。 |
建置和執行此範例
在建置範例之前,您需要進行四個程式碼修改,以自訂 BizTalk 伺服器的範例。 這是必要的,因為範例會使用任意名稱來傳送與合作物件相關聯的埠,以及登記的任意角色名稱。 因此,您必須提供有效的名稱給範例。 為了示範此範例,本主題描述先從下列目錄建置 PartyResolution 範例: <Samples Path> \Orchestrations\PartyResolution。 此方法可用來確保 BizTalk 伺服器上存在有效的角色名稱和傳送埠名稱,以示範範例程式。
建置此範例
首先,請確定已建置並初始化 PartyResolution 範例,以便範例可以使用有效的角色名稱和傳送埠名稱。 本文記載于PartyResolution (BizTalk Server Sample) 中名為「建置和初始化此範例」一節。
在 Visual Studio 中,開啟方案檔 PartnerManagement.sln。
在 方案總管 中,開啟原始程式檔 PartnerManagement.cs。
向下捲動至名為 CreateParty 的函式,並從 PartyResolution 範例插入兩個傳送埠名稱,或使用 BizTalk 伺服器環境的有效名稱。 下列程式碼範例示範如何使用 PartyResolution 範例中的傳送埠進行變更。
// Replacing arbitrary send port names with PartyResolution send port names === // myParty.SendPorts.Add(catalog.SendPorts["NormalDelivery"]); // myParty.SendPorts.Add(catalog.SendPorts["ExpressDelivery"]); myParty.SendPorts.Add(catalog.SendPorts["SP_FilesToShipmentAgency1"]); myParty.SendPorts.Add(catalog.SendPorts["SP_FilesToShipmentAgency2"]);
向下捲動至名為 EnlistParty 的函式,並變更 foreach 迴圈,以便搜尋 Supplier 元件中名為 「ShipmentRole」 的角色,以與登記搭配使用。 下列程式碼範例示範從 PartyResolution 範例使用 ShipmentRole 的變更。
// Search for the “Shipmentrole” instead of “shipperRole” Role svcRole = null; //=== foreach (Role role in catalog.Assemblies[0].Roles) foreach (Role role in catalog.Assemblies["Supplier"].Roles) { //=== if (role.Name == "ShipperRole") if (role.Name == "ShipmentRole") { svcRole = role; break; } }
向下捲動至名為 UnenlistParty 的函式,並變更 foreach 迴圈以搜尋 ShipmentRole 的 Supplier 元件。 下列程式碼範例示範這項變更。
// Search for the “ShipmentRole” instead of “shipperRole” Role svcRole = null; //=== foreach (Role role in catalog.Assemblies[0].Roles) foreach (Role role in catalog.Assemblies["Supplier"].Roles) { //=== if (role.Name == "ShipperRole") if (role.Name == "ShipmentRole") { svcRole = role; break; } }
使用 ShipmentRole 建立並登記新合作物件之後,此範例的設計目的是要立即取消登錄合作物件並加以刪除。 將下列程式碼變更新增至主要程式以暫停執行,並可讓您在 BizTalk Server 管理主控台中檢視新合作物件的已建立登記。
static void Main(string[] args) { CreateParty(); EnlistParty(); Console.WriteLine("\nNew Party created and enlisted.\n\nPress <Enter> to unenlist and delete.\n"); Console.ReadLine(); UnenlistParty(); DeleteParty(); }
在 [建置] 功能表上,按一下 [建置方案]。
執行此範例
開啟命令視窗並巡覽至下列資料夾:
<範例路徑>\管理員\ExplorerOM\PartnerManagement\bin\Debug
執行檔案 PartnerManagement.exe。
當範例顯示新合作物件的建立和登記訊息時,請開啟 BizTalk Server 管理主控台,並在[合作物件] 節點下檢視名為FedEx的新合作物件。
在 BizTalk Server 管理主控台中,流覽樹狀檢視至Applications\BizTalk Application 1\Role Links下的ShipmentRole。 按兩下[ShipmentRole],並注意已對應至SupplierAdvice作業的ShipmentAgency1,並注意已對應至登記中SupplierOrder作業的ShipmentAgency2。
在命令視窗中,按 ENTER 以允許範例取消登錄並刪除新合作物件。
在 BizTalk Server 管理主控台中,確認合作物件已從ShipmentRole取消登錄,並從[合作物件] 節點中刪除。
Windows PowerShell 指令碼範例
下列Windows PowerShell腳本範例可用來示範ExplorerOM類別的相同功能:
#===============================================================#
#=== ===#
#=== Create a new party named "FedEx" as an example. ===#
#=== ===#
#=== Two Shipment agency send ports from the PartyResolution ===#
#=== sample will be added to the party. ===#
#=== ===#
#===============================================================#
Function CreateParty
{
#=== Create a party =====================#
$myParty = $Catalog.AddNewParty()
$myParty.Name = "FedEx"
#=== create a standard alias ==========================#
$standardAlias = $myParty.AddNewAlias()
$standardAlias.Name = "D-U-N-S (Dun & Bradstreet)"
$standardAlias.Value = "Value1"
#=== Create a custom alias ==================#
$customAlias = $myParty.AddNewAlias()
$customAlias.Name = "Telephone"
$customAlias.Qualifier = "100"
$customAlias.Value = "4255550234"
#=== Add party send ports =====================================================#
#===============================================================#
#=== Have to use IList directly on this sendports collection ===#
#=== to work around a PowerShell issue. ===#
#===============================================================#
$iList = [System.Collections.IList]
$sendport1 = $Catalog.SendPorts["SP_FilesToShipmentAgency1"]
[void] $iList.GetMethod("Add").Invoke($myParty.SendPorts,$sendport1)
$sendport2 = $Catalog.SendPorts["SP_FilesToShipmentAgency2"]
[void] $iList.GetMethod("Add").Invoke($myParty.SendPorts,$sendport2)
#=== Specify a signature certificate, make sure the certificate is available ===#
#=== in the AddressBook store of the Local Machine ===#
foreach ($certificate in $Catalog.Certificates)
{
if ($certificate.ShortName -eq "BR, Certisign Certificadora Digital Ltda., Certisign - Autoridade Certificadora - AC2")
{
$myParty.SignatureCert = $certificate
}
}
#=== Commit the changes ===#
$catalog.SaveChanges()
}
#===============================================================#
#=== ===#
#=== Delete the party named "FedEx" ===#
#=== ===#
#===============================================================#
Function DeleteParty
{
$Catalog.RemoveParty($Catalog.Parties["FedEx"])
#=== Commit the changes ===#
$catalog.SaveChanges()
}
#===============================================================#
#=== ===#
#=== Enlist the "FedEx" party with the "ShipmentRole" ===#
#=== ===#
#===============================================================#
Function EnlistParty
{
$myParty = $Catalog.Parties["FedEx"]
#=== Get the "ShipmentRole" in the Supplier assembly. ===#
$svcRole = $Catalog.Assemblies["Supplier"].Roles["ShipmentRole"]
#=== Enlist the party under the shipper role ===#
$enlistedParty = $svcRole.AddNewEnlistedParty($myParty)
#===============================================================#
#=== Have to use IList directly on this sendports collection ===#
#=== to work around a PowerShell issue. ===#
#===============================================================#
$iList = [System.Collections.IList]
#===============================================================#
#=== Example port to be used for the role's "SupplierAdvice" ===#
#=== operation. ===#
#=== Using the first send port added to the new party which ===#
#=== is "SP_FilesToShipmentAgency1" at index 0 in the ===#
#=== sendports collection. ===#
#===============================================================#
$enlistedParty.Mappings[0].SendPort = $iList.GetProperty("Item").GetValue($myParty.SendPorts,0)
#===============================================================#
#=== Example port to be used for the role's "SupplierOrder" ===#
#=== operation. ===#
#=== Using the second send port added to the new party which ===#
#=== is "SP_FilesToShipmentAgency2" at index 1 in the ===#
#=== sendports collection. ===#
#===============================================================#
$enlistedParty.Mappings[1].SendPort = $iList.GetProperty("Item").GetValue($myParty.SendPorts,1)
#=== Commit the changes ===#
$Catalog.SaveChanges()
}
#===============================================================#
#=== ===#
#=== Unenlist the "FedEx" party from the ShipmentRole ===#
#=== ===#
#===============================================================#
Function UnenlistParty
{
#=== Get the "ShipmentRole" from the Supplier assembly. ===#
$svcRole = $Catalog.Assemblies["Supplier"].Roles["ShipmentRole"]
#=== Remove the "FedEx" party ===#
foreach ($enlistedparty in $svcRole.EnlistedParties)
{
if ($enlistedparty.Party.Name -eq "FedEx")
{
$svcRole.RemoveEnlistedParty($enlistedparty)
break
}
}
#=== Commit the changes ===#
$Catalog.SaveChanges()
}
#===================#
#=== Main Script ===#
#===================#
#=== Make sure the ExplorerOM assembly is loaded ===#
[void] [System.reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
#=== Connect to the BizTalk Management database ===#
$Catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
$Catalog.ConnectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"
#=== This sample expects the PartyResolution sample to be built and deployed ===#
#=== to the BizTalk Server. ===#
write-host `r`nMake sure the "PartyResolution" sample application from the Orchestrations
write-host sample directory is deployed and running.
write-host By default it will be listed in the BizTalk Server Administration Console
write-host with the application name: `"BizTalk.Application.1`"`r`n
$SupplierAssembly = $Catalog.Assemblies["Supplier"]
#==================================================================#
#=== Register a trap handler to discard changes on exceptions ===#
#=== Execution will continue in the event we want to unenlist, ===#
#=== and delete the party. ===#
#==================================================================#
$Script:NoExceptionOccurred = $true
$ErrorActionPreference="silentlycontinue"
trap
{
$Script:NoExceptionOccurred = $false
"Exception encountered:`r`n"; $_; "`r`nDiscarding Changes and continuing execution so we can attempt to clean up the party...`r`n"
$Catalog.DiscardChanges()
}
CreateParty
EnlistParty
if ($Script:NoExceptionOccurred)
{
Write-Host "`r`nExample Party `"FedEx`" should be created and enlisted with the `"ShipmentRole`".`r`n"
Write-Host You can view the results in the BizTalk Server Administration console.`r`n
Write-Host "Press <Enter> to unenlist and delete...`r`n"
Read-Host
}
UnenlistParty
DeleteParty
以下是執行 PowerShell 範例腳本的範例輸出。 如果腳本無法執行,請確定已根據本主題頂端的需求附注啟用 PowerShell 腳本。
PS C:\> .\PartnerManagement.ps1
Make sure the PartyResolution sample application from the Orchestrations
sample directory is deployed and running.
By default it will be listed in the BizTalk Server Administration Console
with the application name: "BizTalk.Application.1"
Example Party "FedEx" should be created and enlisted with the "ShipmentRole".
You can view the results in the BizTalk Server Administration console.
Press <Enter> to unenlist and delete...