共用方式為


HOW TO:匯入自訂原則判斷提示

原則判斷提示描述服務端點的功能與需求。用戶端應用程式可使用服務中繼資料中的原則判斷提示,來設定用戶端繫結或自訂服務端點的服務合約。

您可以藉由實作 System.ServiceModel.Description.IPolicyImportExtension 介面並將該物件傳遞至中繼資料系統,或藉由在應用程式組態檔中註冊實作類型,來匯入自訂原則判斷提示。IPolicyImportExtension 介面的實作必須提供預設建構函式。

若要匯入自訂原則判斷提示

  1. 在類別上實作 System.ServiceModel.Description.IPolicyImportExtension 介面。請參閱下列程序。

  2. 透過以下方式插入自訂原則匯入工具:

  3. 使用組態檔。請參閱下列程序。

  4. 將組態檔與 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 搭配使用。請參閱下列程序。

  5. 以程式設計方式插入原則匯入工具。請參閱下列程序。

實作任何類別上的 System.ServiceModel.Description.IPolicyImportExtension 介面

  1. System.ServiceModel.Description.IPolicyImportExtension.ImportPolicy(System.ServiceModel.Description.MetadataImporter,System.ServiceModel.Description.PolicyConversionContext) 方法中,對於每個您感興趣的原則主體,在傳遞至方法的 System.ServiceModel.Description.PolicyConversionContext 物件上呼叫適當的方法 (視您要的判斷提示範圍而定) 來尋找您要匯入的原則判斷提示。下列程式碼範例說明如何使用 System.ServiceModel.Description.PolicyAssertionCollection.Remove(System.String,System.String) 方法找到自訂原則判斷提示,並用一個步驟將它從集合中移除。如果您使用移除方法找到並移除判斷提示,就不必執行步驟 4。

    ' Locate the custom assertion and remove it.
    Dim customAssertion As XmlElement = context.GetBindingAssertions().Remove(name1, ns1)
    If customAssertion IsNot Nothing Then
      Console.WriteLine("Removed our custom assertion from the imported " & "assertions collection and inserting our custom binding element.")
      ' Here we would add the binding modification that implemented the policy.
      ' This sample does not do this.
      Console.ForegroundColor = ConsoleColor.Red
      Console.WriteLine(customAssertion.NamespaceURI & " : " & customAssertion.Name)
      Console.WriteLine(customAssertion.OuterXml)
      Console.ForegroundColor = ConsoleColor.Gray
    End If
    
    // Locate the custom assertion and remove it.
    XmlElement customAssertion = context.GetBindingAssertions().Remove(name1, ns1);
    if (customAssertion != null)
    {
      Console.WriteLine(
        "Removed our custom assertion from the imported "
        + "assertions collection and inserting our custom binding element."
      );
      // Here we would add the binding modification that implemented the policy.
      // This sample does not do this.
      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine(customAssertion.NamespaceURI + " : " + customAssertion.Name);
      Console.WriteLine(customAssertion.OuterXml);
      Console.ForegroundColor = ConsoleColor.Gray;
    }
    
  2. 處理原則判斷提示。請注意,原則系統不會正規化巢狀原則和 wsp:optional。您必須在原則匯入延伸實作中處理這些建構函式。

  3. 對支援原則判斷提示所指定能力或需求的繫結或合約執行自訂。判斷提示通常會指出繫結需要特別的組態或特定的繫結項目。透過存取 System.ServiceModel.Description.PolicyConversionContext.BindingElements 屬性進行這些修改。其他判斷提示需要您修改合約。您可以使用 System.ServiceModel.Description.PolicyConversionContext.Contract 屬性存取和修改合約。請注意,您的原則匯入工具可以為同樣的繫結和合約呼叫多次,但如果無法匯入原則替代選項,則會以不同的原則替代。您的程式碼應回復到此行為。

  4. 從判斷提示集合移除自訂原則判斷提示。如果您沒有移除判斷提示,Windows Communication Foundation (WCF) 會假設原則匯入不成功,而不會匯入有關聯的繫結。如果您使用 System.ServiceModel.Description.PolicyAssertionCollection.Remove(System.String,System.String) 方法找到自訂原則判斷提示,並用一個步驟將它從集合中移除,就不必執行這個步驟。

若要使用組態檔將自訂原則匯入工具插入中繼資料系統

  1. 將匯入工具類型新增至用戶端組態檔中 <policyImporters> 項目內的 <extensions> 項目。

    <client>
        <endpoint 
          address="https://localhost:8080/StatefulService" 
          binding="wsHttpBinding"
          bindingConfiguration="CustomBinding_IStatefulService" 
          contract="IStatefulService"
          name="CustomBinding_IStatefulService" />
      <metadata>
        <policyImporters>
          <extension type="Microsoft.WCF.Documentation.CustomPolicyImporter, PolicyExtensions"/>
        </policyImporters>
      </metadata>
    </client>
    
  2. 在用戶端應用程式中,使用 System.ServiceModel.Description.MetadataResolverSystem.ServiceModel.Description.WsdlImporter 來解析中繼資料,匯入工具會自動叫用。

    ' Download all metadata. 
    Dim endpoints As ServiceEndpointCollection = MetadataResolver.Resolve(GetType(IStatefulService), New EndpointAddress("https://localhost:8080/StatefulService/mex"))
    
    // Download all metadata. 
    ServiceEndpointCollection endpoints
      = MetadataResolver.Resolve(
        typeof(IStatefulService),
        new EndpointAddress("https://localhost:8080/StatefulService/mex")
      );
    

若要使用 Svcutil.exe 將自訂原則匯入工具插入中繼資料系統

  1. 將匯入工具類型新增至 Svcutil.exe.config 組態檔中 <policyImporters> 項目內的 <extensions> 項目。您也可以使用 /svcutilConfig 選項指示 Svcutil.exe 載入在不同組態檔中註冊的原則匯入工具。

  2. 使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 以匯入中繼資料,匯入工具會自動叫用。

若要以程式設計方式將自訂原則匯入工具插入中繼資料系統

  1. 先將匯入工具新增至 System.ServiceModel.Description.MetadataImporter.PolicyImportExtensions 屬性 (例如,如果您目前使用的是 System.ServiceModel.Description.WsdlImporter),再匯入中繼資料。

另請參閱

參考

System.ServiceModel.Description.MetadataResolver
System.ServiceModel.Description.WsdlImporter
System.ServiceModel.Description.MetadataResolver

其他資源

擴充中繼資料系統