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) 将两个对象传递给ImportPolicy方法(a MetadataImporter 和 aPolicyConversionContext)。 通常,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)

定义一个可导入自定义策略断言和添加实现绑定元素的方法。

适用于