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 方法,即 MetadataImporterPolicyConversionContextWindows 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.

备注

通过在配置文件中使用相同的配置元素和选项,你可以将自定义策略导入程序和导出程序与配置的 元数据实用工具工具结合使用 (Svcutil.exe) /svcutilConfig:<configFile>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.

适用于