IPolicyExportExtension 接口

定义

实现 IPolicyExportExtension 可以在 Web Services 描述语言 (WSDL) 信息中插入自定义绑定策略断言。

public interface class IPolicyExportExtension
public interface IPolicyExportExtension
type IPolicyExportExtension = interface
Public Interface IPolicyExportExtension
派生

示例

下面的代码示例演示了如何在 IPolicyExportExtension 上实现 BindingElement。 在此示例中,一个自定义绑定元素在绑定级别附加到了 WSDL 文件。

#region IPolicyExporter Members
public void ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
{
  if (exporter == null)
    throw new NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.");
  if (policyContext == null)
    throw new NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.");

  XmlElement elem = doc.CreateElement(name1, ns1);
  elem.InnerText = "My custom text.";
  XmlAttribute att = doc.CreateAttribute("MyCustomAttribute", ns1);
  att.Value = "ExampleValue";
  elem.Attributes.Append(att);
  XmlElement subElement = doc.CreateElement("MyCustomSubElement", ns1);
  subElement.InnerText = "Custom Subelement Text.";
  elem.AppendChild(subElement);
  policyContext.GetBindingAssertions().Add(elem);
  Console.WriteLine("The custom policy exporter was called.");
}
#endregion
#Region "IPolicyExporter Members"
Public Sub ExportPolicy(ByVal exporter As MetadataExporter, ByVal policyContext As PolicyConversionContext) Implements IPolicyExportExtension.ExportPolicy
  If exporter Is Nothing Then
    Throw New NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.")
  End If
  If policyContext Is Nothing Then
    Throw New NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.")
  End If

  Dim elem As XmlElement = doc.CreateElement(name1, ns1)
  elem.InnerText = "My custom text."
  Dim att As XmlAttribute = doc.CreateAttribute("MyCustomAttribute", ns1)
  att.Value = "ExampleValue"
  elem.Attributes.Append(att)
  Dim subElement As XmlElement = doc.CreateElement("MyCustomSubElement", ns1)
  subElement.InnerText = "Custom Subelement Text."
  elem.AppendChild(subElement)
  policyContext.GetBindingAssertions().Add(elem)
  Console.WriteLine("The custom policy exporter was called.")
End Sub
#End Region

下面的代码示例演示了 System.ServiceModel.Configuration.BindingElementExtensionElement 实现,这可实现从应用程序配置文件加载前面的策略导出程序。

public class ExporterBindingElementConfigurationSection : BindingElementExtensionElement
{
  public ExporterBindingElementConfigurationSection()
  { Console.WriteLine("Exporter configuration section created."); }

  public override Type BindingElementType
  { get { return typeof(ExporterBindingElement); } }

  protected override BindingElement CreateBindingElement()
  { return new ExporterBindingElement(); }
}
Public Class ExporterBindingElementConfigurationSection
    Inherits BindingElementExtensionElement
  Public Sub New()
      Console.WriteLine("Exporter configuration section created.")
  End Sub

  Public Overrides ReadOnly Property BindingElementType() As Type
      Get
          Return GetType(ExporterBindingElement)
      End Get
  End Property

  Protected Overrides Function CreateBindingElement() As BindingElement
      Return New ExporterBindingElement()
  End Function
End Class

下面的示例演示了加载自定义策略导出程序的宿主配置文件。

<system.serviceModel>
  <services>
    <service 
      name="Microsoft.WCF.Documentation.StatefulService"
      behaviorConfiguration="addMetadata"
    >
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/StatefulService"/>
        </baseAddresses>
      </host>
      <endpoint 
        address="http://localhost:8080/StatefulService" 
        binding="customBinding" 
        bindingConfiguration="exporter" 
        contract="Microsoft.WCF.Documentation.IStatefulService" 
      />
      <endpoint
        address="mex"
        binding="mexHttpBinding"
        contract="IMetadataExchange"
      />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="addMetadata">
        <serviceMetadata
           httpGetEnabled="true"
           httpGetUrl=""
         />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <customBinding>
      <!-- 
        Use the name attribute of the binding element as 
        the value of the bindingConfiguration attribute in 
        your endpoint.
      -->
      <binding name ="exporter">
        <!-- Use the name attribute of your binding element extension specified below. -->
        <!-- Be certain the order of your custom binding elements is correct. -->
        <exporterBinding />
        <reliableSession/>
        <textMessageEncoding messageVersion="Default" />
        <httpTransport/>
      </binding>
    </customBinding>
  </bindings>
  <extensions>
    <bindingElementExtensions>
      <!-- Use the add element to associate your bindingelement configuration handler and give it a name to use. -->
      <add 
        type="Microsoft.WCF.Documentation.ExporterBindingElementConfigurationSection,PolicyExtensions" 
        name="exporterBinding" />
    </bindingElementExtensions>
  </extensions>
</system.serviceModel>

下面的示例演示了 WSDL 文件中的自定义断言。

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"

  namespaces removed here for clarity...

>
  <wsp:Policy wsu:Id="CustomBinding_IStatefulService_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <acme 
          b:MyCustomAttribute="ExampleValue"
          xmlns="http://Microsoft/WCF/Documentation/CustomPolicyAssertions"           xmlns:b="http://Microsoft/WCF/Documentation/CustomPolicyAssertions">
           My custom text.
          <MyCustomSubElement>Custom Subelement Text.</MyCustomSubElement>
        </acme>
        <wsrm:RMAssertion xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm/policy">
          <wsrm:InactivityTimeout Milliseconds="600000" />
          <wsrm:AcknowledgementInterval Milliseconds="200" />
        </wsrm:RMAssertion>
        <wsaw:UsingAddressing />
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsdl:import namespace="http://microsoft.wcf.documentation" location="" />

注解

IPolicyExportExtension 对象实现 System.ServiceModel.Channels.BindingElement 接口可以将有关终结点功能或请求的声明写入由特定终结点公开的 WSDL 信息。 通常,绑定元素可实现某些功能,但并不是必需的。 若要从配置文件加载策略导出程序,请实现 System.ServiceModel.Configuration.BindingElementExtensionElement,它可以返回策略导出程序 BindingElement 对象。

策略导出程序由 Windows Communication Foundation (WCF) 使用策略断言向客户端传达是否存在该自定义绑定要求或终结点功能。

ExportPolicy 方法使用的是 MetadataExporterPolicyConversionContext 对象。 使用 GetBindingAssertions, GetMessageBindingAssertionsGetOperationBindingAssertions 方法以获取从不同范围导出的策略断言的集合。 然后,将您的自定义策略断言对象添加到适当的集合中。

Contract 属性可公开正在导出的终结点的 ContractDescription。 这样,IPolicyExportExtension 扩展就可正确限定它们的导出策略断言的范围。 例如,代码中的安全属性可以将行为添加到指示安全策略断言应该添加到的位置的 ContractDescription

IPolicyExportExtension 机制只支持导出 WSDL 中的策略断言。 若要导出自定义 WSDL 元素,则必须使用 IWsdlExportExtension 机制以直接修改 WSDL。

一旦自定义策略断言附加到了 WSDL 信息,客户端就可以通过使用 IPolicyImportExtension 对象来检测和导入自定义绑定断言。

方法

ExportPolicy(MetadataExporter, PolicyConversionContext)

实现此方法可以导出有关绑定的自定义策略断言。

适用于