Condividi tramite


SendPortGroups (esempio di BizTalk Server)

L'esempio SendPortGroups illustra come enumerare e gestire i gruppi di porte di trasmissione usando le classi di amministrazione Microsoft.BizTalk.ExplorerOM .

Prerequisiti

  • È necessario disporre di BizTalk Server privilegi amministrativi per usare gli oggetti amministrativi in questo esempio.

  • L'esecuzione dello script di Windows PowerShell richiede i criteri di esecuzione di Windows PowerShell. Per altre informazioni, vedere about_Execution_Policies.

Scopo dell'esempio

Questo esempio illustra l'uso delle classi BtsCatalogExplorer e SendPortGroup dello spazio dei nomi Microsoft.BizTalk.ExplorerOM per gestire i gruppi di porte di trasmissione in un ambiente BizTalk Server. L'esempio è scritto in Microsoft Visual C#. In questo argomento è inoltre incluso uno script di esempio Windows PowerShell. Nell'esempio vengono illustrate le operazioni seguenti:

  • Connessione al database di gestione BizTalk tramite la classe BtsCatalogExplorer .

  • Creazione di un nuovo gruppo di porte di trasmissione denominato “My Send Port Group”.

  • Enumerazione di gruppi di porte di trasmissione per visualizzare il nuovo gruppo di porte di trasmissione creato.

  • Eliminazione del nuovo gruppo di porte di trasmissione.

    Altre funzioni sono presenti nell'esempio, ma non vengono eseguite nella versione di Visual C#. Alcune delle funzioni aggiuntive vengono illustrate nello script di esempio PowerShell. Le funzioni aggiuntive illustrano le funzionalità seguenti:

  • Aggiunta di una porta di trasmissione al nuovo gruppo di porte di trasmissione creato (descritta nell'esempio relativo a PowerShell).

  • Eliminazione di una porta di trasmissione dal gruppo di porte di trasmissione (descritta nell'esempio relativo a PowerShell).

  • Avvio del gruppo di porte di trasmissione.

  • Arresto del gruppo di porte di trasmissione.

Percorso dell'esempio

L'esempio è disponibile nel seguente percorso dell'SDK:

<Percorso esempi>\Amministrazione\ExplorerOM\SendPortGroups

Nella seguente tabella sono riportati i file inclusi nell'esempio e ne viene descritto lo scopo.

File Descrizione
SendPortGroups.cs File di origine Visual C# per le operazioni illustrate in questo esempio.
SendPortGroups.sln, SendPortGroups.csproj, SendPortGroups.suo File di soluzione e di progetto per l'esempio.

Compilazione ed esecuzione dell'esempio

Per generare l'esempio

  1. In Visual Studio aprire il file della soluzione SendPortGroups.sln.

  2. In Esplora soluzioni fare doppio clic su SendPortGroups.cs per aprire il codice di esempio.

  3. root.ConnectionString Trovare nella CreateSendPortGroup funzione . È necessario modificare la specifica del server in modo che faccia correttamente riferimento al server SQL Server che ospita il database di gestione BizTalk. È inoltre possibile utilizzare un punto (.) per connettersi al server SQL Server locale. Ad esempio:

    root.ConnectionString = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=.";
    
  4. Ripetere il passaggio 3 per le funzioni seguenti:

    • EnumerateSendPortGroups

    • DeleteSendPortGroup

  5. Salvare SendPortGroups.cs.

  6. Scegliere Compila dal menu principale e quindi fare clic su Compila soluzione.

Per eseguire questo esempio

  1. Aprire una finestra di comando e passare alla cartella seguente:

    <Percorso esempi>\Amministrazione\ExplorerOM\SendPortGroups\bin\Debug

  2. Eseguire il file SendPortGroups.exe.

Esempio di script di Windows PowerShell

Per illustrare le stesse funzionalità delle classi ExplorerOM, è possibile usare il frammento di script seguente Windows PowerShell:

Function CreateSendPortGroup($Catalog, $strName)
{
   #=== Register a trap handler to discard changes on exceptions ===#

   $ErrorActionPreference="silentlycontinue"
   trap { "Exception encountered:`r`n"; $_; "`r`nDiscarding Changes.`r`n";$Catalog.DiscardChanges();exit; }

   #=== Create a new send port group and set its name ===#

   $NewSendPortGroup = $Catalog.AddNewSendPortGroup()
   $NewSendPortGroup.Name = $strName

   #=== Persist new ports to the BizTalk Management database ===#

   Write-Host Adding "`"$($NewSendPortGroup.Name)`"..."
   $catalog.SaveChanges();
   Write-Host "`r`nCreateSendPortGroup() completed."
}

Function DeleteSendPortGroup($Catalog,$strName)
{
   #=== Register a trap handler to discard changes on exceptions ===#

   $ErrorActionPreference="silentlycontinue"
   trap { "Exception encountered DeleteSendPortGroup:`r`n"; $_; "`r`nDiscarding Changes.`r`n";$Catalog.DiscardChanges();}

   #=== Delete this sample's new send ports by name ===#

   $NewSendPortGroup = $Catalog.SendPortGroups[$strName]
   $Catalog.RemoveSendPortGroup($NewSendPortGroup)

   #=== Persist changes to the BizTalk Management database ===#

   Write-Host "Deleting `"$strName`"..."
   $catalog.SaveChanges();
   Write-Host "DeleteSendPortGroup() completed."
}

Function EnumerateSendPortGroups($Catalog)
{
   #=== Register a trap handler to discard changes on exceptions ===#

   $ErrorActionPreference="silentlycontinue"
   trap { "Exception encountered During Enumeration:`r`n"; $_; "`r`n"}

   #=== Display all send port groups by name ===#

   $count = 1
   foreach ($group in $catalog.SendPortGroups)
   {
     Write-Host "$count. $($group.Name)"
     $count++
   }

   Write-Host "`r`nEnumerateSendPortGroups() completed."
}

Function PromptForSendPort($Catalog)
{
   #=== Provide a list of the send ports for the user to make a selection ===#

   Write-Host "Here is a list of send ports:`r`n"
   $count = 1
   foreach($sendport in $Catalog.SendPorts)
   {
      Write-Host ($Count). ($Sendport.Name)
      $count++
   }

   Write-Host "`r`nChoose a port to add to the group by ordinal (ex. 1,2, etc): " -nonewline
   $selection = read-host
   $selection = $Catalog.SendPorts[([int]$selection)-1]
   Write-Host $Selection.Name selected.

   return $Selection.Name
}

Function AddSendPortToSendPortGroup($Catalog,$strPortName,$strGroupName)
{

   #=== Add the user's selected send port to the group ===#

   #========================================================#
   #=== Presently, we have to use WMI from PowerShell    ===#
   #=== This is because the methods on the collections   ===#
   #=== are marked internal. So unfortunately the        ===#
   #=== following very straightforward code won't work.  ===#
   #========================================================#

   # $sendportgroup = $Catalog.SendPortGroups[$strName]
   # $sendportgroup.SendPorts.Add($Catalog.SendPorts[$strPortName])
   # $catalog.SaveChanges();

   #============================================================================#
   #=== Get the WMI send port group representation to look up DB and server. ===#
   #=== Expecting only one to match the query in this example.               ===#
   #============================================================================#

   $WQL = "select * from MSBTS_SendPortGroup where Name='" + $strGroupName + "'"
   $groupset = gwmi -query ($WQL) -namespace "root\MicrosoftBizTalkServer"

   #=== Create a new MSBTS_SendPortGroup2SendPort instance to map the port to the group ===#

   $group2port = [WMICLASS]"root\MicrosoftBizTalkServer:MSBTS_SendPortGroup2SendPort"
   $newPortEntry = [WMI] $group2port.psbase.CreateInstance()
   $newPortEntry.MgmtDbServerOverride = $groupset.MgmtDbServerOverride
   $newPortEntry.MgmtDbNameOverride = $groupset.MgmtDbNameOverride
   $newPortEntry.SendPortGroupName = $groupset.Name
   $newPortEntry.SendPortName = $strPortName
   Write-Host "Adding $strPortName to $($groupset.Name)"

   #=== Persist changes to the BizTalk Management database ===#

   #=== POWERSHELL V1 BUG WORKAROUND =============================#
   #=== First method call on a non-cimv2 WMI object can fail.  ===#
   #=== The workaround in PowerShell V1 is to call GetType()   ===#
   #=== as the first method.                                   ===#
   $ErrorActionPreference="silentlycontinue"
   trap {}
   $newPortEntry.GetType()
   #=== POWERSHELL V1 BUG WORKAROUND =============================#

   $ErrorActionPreference="continue"
   [void] $newPortEntry.Put()

   #=== Since we used WMI to update the BizTalk Management database, ===#
   #=== refresh our BtsExplorerCatalog to have an updated DB view.   ===#

   $Catalog.Refresh()
   Write-Host "AddSendPortToSendPortGroup() completed."
}

Function DeleteSendPortFromSendPortGroup($Catalog, $strPortName, $strGroupName)
{
   #========================================================#
   #=== Presently, we have to use WMI from PowerShell    ===#
   #=== This is because the methods on the collections   ===#
   #=== are marked internal. So unfortunately the        ===#
   #=== following very straightforward code won't work.  ===#
   #========================================================#

   # $sendportgroup = $Catalog.SendPortGroups[$strGroupName]
   # $sendportgroup.SendPorts.Remove($Catalog.SendPorts[$strPortName])
   # $catalog.SaveChanges();

   #============================================================================#
   #=== Get the WMI send port to group instance and delete it.               ===#
   #=== Expecting only one to match the query in this example.               ===#
   #============================================================================#

   $WQL = "select * from MSBTS_SendPortGroup2SendPort where " +
          "SendPortName='" + $strPortName + "' and " +
          "SendPortGroupName='" + $strGroupName + "'"

   $groupset = gwmi -query ($WQL) -namespace "root\MicrosoftBizTalkServer"

   write-host "Removing $strPortName from $strGroupName..."
   $groupset.Delete();

   #=== Since we used WMI to update the BizTalk Management database, ===#
   #=== refresh our BtsExplorerCatalog to have an updated DB view.   ===#

   $Catalog.Refresh()
   Write-Host "DeleteSendPortFromSendPortGroup() completed."
}

#========================#
#=== Main Script Body ===#
#========================#

#=== 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 = "DATABASE=BizTalkMgmtDb;Integrated Security=SSPI;SERVER=."

#=== Exercise the CreateSendPortGroup function to create a new send port group ===#

$SendPortGroupName = "PowerShell Sample Send Port Group"

Write-Host "`r`n============================="
Write-Host "=== CreateSendPortGroup() ==="
Write-Host "=============================`r`n"
CreateSendPortGroup $Catalog $SendPortGroupName

#=== Enumerate all send port groups to view the new one ===#

Write-Host "`r`n================================="
Write-Host "=== EnumerateSendPortGroups() ==="
Write-Host "=================================`r`n"
EnumerateSendPortGroups $Catalog

#=== Add a send port to the group ===#

Write-Host "`r`n===================================="
Write-Host "=== AddSendPortToSendPortGroup() ==="
Write-Host "====================================`r`n"
$SendPortName = PromptForSendPort($Catalog)
AddSendPortToSendPortGroup $Catalog $SendPortName $SendPortGroupName

#=== Remove the send port from the group ===#

Write-Host "`r`n========================================="
Write-Host "=== DeleteSendPortFromSendPortGroup() ==="
Write-Host "=========================================`r`n"
DeleteSendPortFromSendPortGroup $Catalog $SendPortName $SendPortGroupName

#=== Delete the group ===#

Write-Host "`r`n============================="
Write-Host "=== DeleteSendPortGroup() ==="
Write-Host "=============================`r`n"
DeleteSendPortGroup $Catalog $SendPortGroupName

Write-Host

Di seguito è riportato l'output di esempio previsto in seguito all'esecuzione dell'esempio di script Windows PowerShell.

PS C:\> .\sendportgroups.ps1

=============================
=== CreateSendPortGroup() ===
=============================

Adding "PowerShell Sample Send Port Group"...

CreateSendPortGroup() completed.

=================================
=== EnumerateSendPortGroups() ===
=================================

1. PowerShell Sample Send Port Group

EnumerateSendPortGroups() completed.

====================================
=== AddSendPortToSendPortGroup() ===
====================================

Here is a list of send ports:

1 . ResendPort
2 . HelloWorldSendPort
3 . ToCustomerSendPort
4 . CBRUSSendPort
5 . CBRCANSendPort
6 . SendportCANOrders

Choose a port to add to the group by ordinal (ex. 1,2, etc): 2
HelloWorldSendPort selected.
Adding HelloWorldSendPort to PowerShell Sample Send Port Group
AddSendPortToSendPortGroup() completed.

=========================================
=== DeleteSendPortFromSendPortGroup() ===
=========================================

Removing HelloWorldSendPort from PowerShell Sample Send Port Group...
DeleteSendPortFromSendPortGroup() completed.

=============================
=== DeleteSendPortGroup() ===
=============================

Deleting "PowerShell Sample Send Port Group"...
DeleteSendPortGroup() completed.

Vedere anche

Admin-ExplorerOM (cartella di esempi di BizTalk Server)