IPolicyExportExtension 介面

定義

實作 IPolicyExportExtension,將自訂繫結原則判斷提示插入 Web Services Description Language (WSDL) 資訊中。

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

範例

下列程式碼範例會示範 IPolicyExportExtensionBindingElement 的實作。 在此範例中,自訂繫結項目是在繫結層級附加至 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 物件。 您可以使用 GetBindingAssertionsGetMessageBindingAssertionsGetOperationBindingAssertions 方法,取得已經在各種範圍匯出的原則判斷提示的集合。 然後將您的自訂原則判斷提示物件加入至適當的集合。

Contract 屬性會公開正在匯出的端點的 ContractDescription。 這可讓 IPolicyExportExtension 延伸正確限定其匯出原則判斷提示的範圍。 例如,程式碼中的安全性屬性可能會將行為加入至 ContractDescription,指出應在何處加入安全性原則判斷提示。

IPolicyExportExtension 機制僅支援以 WSDL 匯出原則判斷提示。 如果要匯出自訂 WSDL 項目,您必須使用 IWsdlExportExtension 機制直接修改 WSDL。

在自訂原則判斷提示附加至 WSDL 資訊之後,用戶端就可以使用 IPolicyImportExtension 物件來偵測及匯入自訂繫結判斷提示。

方法

ExportPolicy(MetadataExporter, PolicyConversionContext)

請實作以包含,以便匯出有關繫結的自訂原則判斷提示。

適用於