IPolicyImportExtension 介面

定義

定義物件的方法,該物件會匯入有關繫結的自訂原則判斷提示。Defines a method for objects that import custom policy assertions about bindings.

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

範例

下列程式碼範例會示範如何使用 PolicyAssertionCollection.Remove 方法在一個步驟中找出、傳回和移除判斷提示。The following code example shows the use of the PolicyAssertionCollection.Remove method to locate, return, and remove the assertion in one step.

  #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 時,用戶端應用程式組態檔載入自訂原則匯入工具。The following code example shows the client application configuration file to load the custom policy importer when the System.ServiceModel.Description.MetadataResolver is invoked.

<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 來下載並將中繼資料解析成描述物件。The following code example shows the use of the MetadataResolver to download and resolve metadata into description objects.

// 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 資訊中搜尋有關端點功能或需求的自訂原則判斷提示。Implement the IPolicyImportExtension interface to search WSDL information exposed by a particular endpoint for custom policy assertions about endpoint capabilities or requirements. 一般而言,原則匯入工具會搜尋特定的判斷提示,以及插入繫結項目、設定繫結項目或修改合約,以支援判斷提示的需求。Typically, a policy importer searches for a specific assertion and either inserts a binding element, configures a binding element, or modifies the contract to support the requirements of the assertion.

和其對應的 IPolicyExportExtension 不同的是,IPolicyImportExtension 不需要由 BindingElement 物件實作;您可以使用如<範例>一節所示的用戶端組態區段載入它,或使用程式設計的方式,將它加入至 System.ServiceModel.Description.WsdlImporter 建構函式。Unlike its counterpart, IPolicyExportExtension, IPolicyImportExtension does not require implementation by a BindingElement object; you can load it using the client configuration section shown in the Examples section or programmatically by adding it to the System.ServiceModel.Description.WsdlImporter constructor.

Windows Communication Foundation (WCF) 會將兩個物件ImportPolicy傳遞給方法MetadataImporter : 和PolicyConversionContextWindows Communication Foundation (WCF) passes two objects to the ImportPolicy method, a MetadataImporter and a PolicyConversionContext. 一般而言,PolicyConversionContext 物件已經包含各繫結範圍的原則判斷提示。Typically the PolicyConversionContext object already contains the policy assertions for each binding scope.

IPolicyImportExtension 實作會執行下列步驟:An IPolicyImportExtension implementation performs the following steps:

  1. 藉由呼叫 GetBindingAssertionsGetMessageBindingAssertionsGetOperationBindingAssertions 方法 (視範圍而定),找出所負責的自訂原則判斷提示。Locates the custom policy assertion for which it is responsible by calling either the GetBindingAssertions, GetMessageBindingAssertions, or GetOperationBindingAssertions methods, depending upon the scope.

  2. 從判斷提示集合移除原則判斷提示。Removes the policy assertion from the assertion collection. PolicyAssertionCollection.Remove 方法只要一個步驟,即可尋找、傳回及移除判斷提示。The PolicyAssertionCollection.Remove method locates, returns, and removes the assertion in one step.

  3. 藉由將必要的自訂 BindingElement 加入至 BindingElements 屬性或修改 PolicyConversionContext.Contract 屬性,來修改繫結堆疊或合約。Modify the binding stack or the contract by either adding a required custom BindingElement to the BindingElements property or by modifying the PolicyConversionContext.Contract property.

步驟 2 是十分重要的。Step 2 is important. 呼叫完所有原則匯入工具之後, WCF 會檢查是否有任何保留的原則判斷提示。After all policy importers have been called, WCF checks for the existence of any policy assertions that remain. 如果有的話, WCF 會假設原則匯入不成功, 而且不會匯入相關聯的系結。If one exists, WCF assumes that the policy import was unsuccessful and does not import the associated binding.

重要

惡意中繼資料提供者在嘗試不當利用原則匯入工具時,可能會嘗試在中繼資料中傳送格式不正確的 XML。A malicious metadata supplier can attempt to send malformed XML as part of metadata in an attempt to exploit a policy importer. 強烈建議自訂原則匯入工具對於可以傳遞給它的所有形式的 XML 都要很穩固。It is strongly recommended that custom policy importers be robust to all forms of XML that can be passed to it.

自訂 MetadataImporter 實作必須實作自己的 PolicyConversionContext 物件,以擷取附加至自訂中繼資料格式的原則判斷提示。Custom MetadataImporter implementations must implement their own PolicyConversionContext object to extract the policy assertions attached to the custom metadata format.

如果要匯出和匯入不是原則判斷提示的自訂 WSDL 項目,請參閱 System.ServiceModel.Description.IWsdlExportExtensionSystem.ServiceModel.Description.IWsdlImportExtensionIf you want to export and import custom WSDL elements that are not policy assertions, see System.ServiceModel.Description.IWsdlExportExtension and System.ServiceModel.Description.IWsdlImportExtension.

注意

您可以使用自訂原則匯入工具, 並使用設定檔和/svcutilConfig:<configFile>選項中的相同設定元素, 搭配system.servicemodel 中繼資料公用程式工具 (Svcutil)來匯出工具。You can use custom policy importers and exporters with the ServiceModel Metadata Utility Tool (Svcutil.exe) by using the same configuration elements in a configuration file and the /svcutilConfig:<configFile> option.

方法

ImportPolicy(MetadataImporter, PolicyConversionContext)

定義可以匯入自訂原則判斷提示及加入實作繫結項目的方法。Defines a method that can import custom policy assertions and add implementing binding elements.

適用於