共用方式為


IPolicyImportExtension 介面

定義

定義物件的方法,該物件會匯入有關繫結的自訂原則判斷提示。

public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
衍生

範例

下列程式碼範例會示範如何使用 PolicyAssertionCollection.Remove 方法在一個步驟中找出、傳回和移除判斷提示。

  #region IPolicyImporter Members
  public const string name1 = "acme";
  public const string ns1 = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions";

  /*
   * Importing policy assertions usually means modifying the bindingelement stack in some way
   * to support the policy assertion. The procedure is:
   * 1. Find the custom assertion to import.
   * 2. Insert a supporting custom bindingelement or modify the current binding element collection
   *     to support the assertion.
   * 3. Remove the assertion from the collection. Once the ImportPolicy method has returned,
   *     any remaining assertions for the binding cause the binding to fail import and not be
   *     constructed.
   */
  public void ImportPolicy(MetadataImporter importer, PolicyConversionContext context)
  {
    Console.WriteLine("The custom policy importer has been called.");
    // 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;
    }
 }
#endregion
    #Region "IPolicyImporter Members"
    Public Const name1 As String = "acme"
    Public Const ns1 As String = "http://Microsoft/WCF/Documentation/CustomPolicyAssertions"

'    
'     * Importing policy assertions usually means modifying the bindingelement stack in some way
'     * to support the policy assertion. The procedure is:
'     * 1. Find the custom assertion to import.
'     * 2. Insert a supporting custom bindingelement or modify the current binding element collection
'     *     to support the assertion.
'     * 3. Remove the assertion from the collection. Once the ImportPolicy method has returned, 
'     *     any remaining assertions for the binding cause the binding to fail import and not be 
'     *     constructed.
'     
    Public Sub ImportPolicy(ByVal importer As MetadataImporter, ByVal context As PolicyConversionContext) Implements IPolicyImportExtension.ImportPolicy
      Console.WriteLine("The custom policy importer has been called.")
      ' 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
    End Sub
  #End Region

下列程式碼範例會示範當叫用 System.ServiceModel.Description.MetadataResolver 時,用戶端應用程式組態檔載入自訂原則匯入工具。

<client>
    <endpoint 
      address="http://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>

下列程式碼範例會示範如何使用 MetadataResolver 來下載並將中繼資料解析成描述物件。

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

備註

您可以實作 IPolicyImportExtension 介面,在特定端點所公開的 WSDL 資訊中搜尋有關端點功能或需求的自訂原則判斷提示。 一般而言,原則匯入工具會搜尋特定的判斷提示,以及插入繫結程序項目、設定繫結程序項目或修改合約,以支援判斷提示的需求。

和其對應的 IPolicyExportExtension 不同的是,IPolicyImportExtension 不需要由 BindingElement 物件實作;您可以使用如<範例>一節所示的用戶端組態區段載入它,或使用程式設計的方式,將它加入至 System.ServiceModel.Description.WsdlImporter 建構函式。

Windows Communication Foundation (WCF) 將兩個 PolicyConversionContext 物件傳遞至 ImportPolicy 方法,以及 MetadataImporter 。 一般而言,PolicyConversionContext 物件已經包含各繫結範圍的原則判斷提示。

IPolicyImportExtension 實作會執行下列步驟:

  1. 藉由呼叫 GetBindingAssertionsGetMessageBindingAssertionsGetOperationBindingAssertions 方法 (視範圍而定),找出所負責的自訂原則判斷提示。

  2. 從判斷提示集合移除原則判斷提示。 PolicyAssertionCollection.Remove 方法只要一個步驟,即可尋找、傳回及移除判斷提示。

  3. 藉由將必要的自訂 BindingElement 加入至 BindingElements 屬性或修改 PolicyConversionContext.Contract 屬性,來修改繫結堆疊或合約。

步驟 2 是十分重要的。 呼叫所有原則匯入工具之後,WCF 會檢查是否有任何保留的原則判斷提示存在。 如果有的話,WCF 會假設原則匯入失敗,而且不會匯入相關聯的系結。

重要

惡意中繼資料提供者在嘗試不當利用原則匯入工具時,可能會嘗試在中繼資料中傳送格式不正確的 XML。 強烈建議自訂原則匯入工具對於可以傳遞給它的所有形式的 XML 都要很穩固。

自訂 MetadataImporter 實作必須實作自己的 PolicyConversionContext 物件,以擷取附加至自訂中繼資料格式的原則判斷提示。

如果要匯出和匯入不是原則判斷提示的自訂 WSDL 項目,請參閱 System.ServiceModel.Description.IWsdlExportExtensionSystem.ServiceModel.Description.IWsdlImportExtension

注意

您可以使用自訂原則匯入工具和匯出工具搭配 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) ,方法是在組態檔和 /svcutilConfig:<configFile> 選項中使用相同的組態專案。

方法

ImportPolicy(MetadataImporter, PolicyConversionContext)

定義可以匯入自訂原則判斷提示及加入實作繫結項目的方法。

適用於