共用方式為


用於偵測服務行為組態位置的 Windows PowerShell 指令碼

WCF 4.0 新增了在組態階層合併多個組態檔之服務行為的功能。此功能可讓您更輕鬆地在高等級的組態檔 (例如網站等級的 web.config) 定義 common 行為,以及在較低等級的組態檔定義額外的行為。下列範例示範如何進行這項工作:

網站等級的 web.config

  <configuration>
    <system.serviceModel>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyBehavior">
            <serviceDebug includeExceptionDetailInFaults="True" />
            <serviceMetadata httpGetEnabled="True" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
  </configuration>

應用程式等級的 web.config

  <configuration>
    <system.serviceModel>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyBehavior">
            <etwTracking profileName="Troubleshooting Tracking Profile" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
  </configuration>

應用程式內部設為使用 “MyBehavior” 的 WCF 服務實際上會繼承 serviceDebug/serviceMetadataetwTracking 組態。如需合併之行為組態的詳細資訊,請參閱 WCF 4.0 組態中的行為合併 (https://go.microsoft.com/fwlink/?LinkId=194422) (可能為英文網頁) 與 .NET 組態合併預設行為 (https://go.microsoft.com/fwlink/?LinkId=194423) (可能為英文網頁)。

儘管合併行為組態的做法具有彈性,但有時候也會造成混淆,因為服務實際上的行為組態可能與本機 web.config 中定義的不同。以下範例示範如何以 Cmdlet 格式撰寫 PowerShell 指令碼,以分析服務行為組態並報告包含實際設定之組態檔的位置。

注意

提供的範例僅適用於教學用途。請勿將範例用於生產環境,因為這些範例並未在生產環境中測試。Microsoft 不對這些範例提供技術支援。

必要條件

使用者應熟悉 Windows PowerShell 指令碼與 AppFabric Cmdlet。

此範例需要下列必要條件:

  • 已安裝 PowerShell v2

  • 已經執行預設的 AppFabric 安裝。

範例位置與檔案

範例檔案包括:

  • Readme.mhtml

  • Code\detectServiceBehaviorConfigLocation.ps1

設定並執行此範例

  1. 下列範例示範如何從 PowerShell 主控台執行指令碼 Cmdlet:

    PS> cd <samples>\Samples\Management\DetectServiceBehaviorConfigLocation\Code
    
    PS> . .\detectServiceBehaviorConfigLocation.ps1 #the first dot is for loading the ps1 as a function library
    
    PS> Get-ServiceBehaviorConfigLocation -SiteName "Default Web Site" -VirtualPath /App/service.svc
    
    Name                                                        Location
    ----                                                        --------
    etwTracking                                                 MACHINE/WEBROOT/APPHOST/Default Web Site
    serviceDebug                                                MACHINE/WEBROOT/APPHOST/Default Web Site/App
    serviceMetadata                                             MACHINE/WEBROOT/APPHOST/Default Web Site/App
    

    注意

    您可能需要將執行原則從 ‘Restricted’ 變更為 ‘RemoteSigned’ 才能執行此範例。

    注意

    Get-ServiceBehaviorConfigLocation Cmdlet 接受兩個參數,這兩個參數指定目標服務的 SiteName 與 VirtualPath,並傳回實際的行為組態設定清單 (其中包含 IIS 路徑格式的組態檔位置)。

  2. Cmdlet 也會透過管道接受 AppFabric 之 Get-ASAppService Cmdlet 的輸出。此外,這使得 Cmdlet 可以處理在指定的範圍中探索到的所有服務,如以下範例所示:

    PS> Get-ASAppService -SiteName "Default Web Site" | 
          foreach-object {$service=$_; Get-ServiceBehaviorConfigLocation $service | 
          select-object @{Name="SiteName"; Expression={$service.SiteName}}, @{Name="VirtualPath"; Expression={$service.VirtualPath}}, "Name", "Location"}
    
    SiteName                      VirtualPath                   Name                          Location
    --------                      -----------                   ----                          --------
    Default Web Site              /App/service.svc            serviceDebug                  MACHINE/WEBROOT/APPHOST/De...
    Default Web Site              /App/service.svc            serviceMetadata               MACHINE/WEBROOT/APPHOST/De...
    Default Web Site              /AnotherApp/HelpRequestT... etwTracking                   MACHINE/WEBROOT
    Default Web Site              /AnotherApp/HelpRequestT... workflowInstanceManagement    MACHINE/WEBROOT
    ...
    

    Get-ASAppService 發現的服務清單會以管線方式傳送給 Get-ServiceBehaviorConfigLocation,並使用 foreach 子句來格式化最後輸出。請注意,它會將每個服務的 SiteNameVirtualPath 新增到原始結果,以將服務和它們的行為設定相互關聯。

移除此範例

  1. 只要關閉 PowerShell 工作階段即可,因為執行此範例不會修改電腦上的任何資源。

示範

此範例的指令碼分成四個區段:

初始化

指令碼的第一個部分可確定已載入所有相依性。包括載入 AppFabric Cmdlet 模組與 Microsoft.Web.Administration 程式庫以利讀取 IIS 組態。

if ((Get-Command -Module ApplicationServer) -eq $null)
{
    Import-Module ApplicationServer
}

[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" ) | Out-Null

Cmdlet 函式

此範例的另一個目標,是示範如何以指令碼 (而非程式碼) 撰寫 Cmdlet。Get-ServiceBehaviorConfigLocation 是定義 Cmdlet 的函式。與 PowerShell 指令碼中的一般函數不同的是,它包含用於定義 Cmdlet 參數集的 Param、用於處理記錄的 Process 邏輯,以及用於執行清理的 End 邏輯。

在範例中,這個 Cmdlet 函式只負責正規化輸入參數並呼叫 GetBehaviorConfigLocationPerService 函式 (此函式含有偵測行為組態設定位置的主要邏輯)。

主要函式

主要函式 GetBehaviorConfigLocationPerService 會先開啟服務範圍的組態檔。範例使用 Microsoft.Web.Administration.dll 中的 Managed 程式碼 API 來讀取 IIS 組態階層中的組態檔。範例會尋找具有相符之服務組態的所有 <behavior> 元素,並列舉每個元素的所有子元素。

函式會維護在列舉期間儲存實際的行為設定清單的雜湊表 ($effectiveChildElementMap)。雜湊表的內容會隨著列舉在組態階層往下進行而發展,例如,如果觀察到 <remove name=”X”/> 元素,將會移除設定 X。一旦列舉完成,雜湊表中的最終值將會對應實際的服務行為組態。

協助程式函式

  • FindBehaviorElementsByName - 尋找 "name" 屬性符合所指定名稱的所有 <behavior> 元素。

  • IsClearTagPresent - 判斷 <clear> 元素是否存在於所指定的 <behavior> 元素下。

  • IsRemoveTagPresent - 判斷具有相符之 "name" 屬性的 <remove> 元素是否存在於所指定的 <behavior> 元素下。

Known Issues/Limitations

  • Microsoft.Web.Administration API 無法判斷 <remove>/<clear> 元素相對於其他元素 (在同一個父 <behavior> 元素下) 的順序。範例假設 <remove> 與 <clear> 元素先出現。

  2011-12-05