Share via


IPolicyExportExtension Antarmuka

Definisi

Terapkan IPolicyExportExtension untuk menyisipkan pernyataan kebijakan pengikatan kustom dalam informasi Web Services Description Language (WSDL).

public interface class IPolicyExportExtension
public interface IPolicyExportExtension
type IPolicyExportExtension = interface
Public Interface IPolicyExportExtension
Turunan

Contoh

Contoh kode berikut menunjukkan implementasi IPolicyExportExtension pada BindingElement. Dalam contoh ini, elemen pengikatan kustom dilampirkan ke file WSDL pada tingkat pengikatan.

#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

Contoh kode berikut menunjukkan System.ServiceModel.Configuration.BindingElementExtensionElement implementasi yang memungkinkan pengekspor kebijakan sebelumnya dimuat dari file konfigurasi aplikasi.

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

Contoh berikut menunjukkan file konfigurasi host yang memuat pengekspor kebijakan kustom.

<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>

Contoh berikut menunjukkan pernyataan kustom dalam file 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="" />

Keterangan

Terapkan IPolicyExportExtension antarmuka pada System.ServiceModel.Channels.BindingElement objek untuk menulis pernyataan tentang kemampuan atau persyaratan titik akhir ke dalam informasi WSDL yang diekspos oleh titik akhir tertentu. Biasanya elemen pengikatan adalah elemen yang mengimplementasikan beberapa fitur, tetapi ini tidak diperlukan. Untuk memuat pengekspor kebijakan Anda dari file konfigurasi, terapkan System.ServiceModel.Configuration.BindingElementExtensionElement yang mengembalikan objek pengekspor BindingElement kebijakan.

Pengekspor kebijakan digunakan oleh Windows Communication Foundation (WCF) untuk menggunakan pernyataan kebijakan untuk berkomunikasi dengan klien keberadaan persyaratan pengikatan kustom atau kemampuan titik akhir tersebut.

Metode ini ExportPolicy mengambil MetadataExporter objek dan PolicyConversionContext . GetBindingAssertionsGunakan metode , GetMessageBindingAssertions, dan GetOperationBindingAssertions untuk mendapatkan kumpulan pernyataan kebijakan yang telah diekspor di berbagai cakupan. Kemudian tambahkan objek pernyataan kebijakan kustom Anda ke koleksi yang sesuai.

Properti Contract memaparkan ContractDescription untuk titik akhir yang sedang diekspor. Hal ini memungkinkan IPolicyExportExtension ekstensi untuk mencakup pernyataan kebijakan yang diekspor dengan benar. Misalnya, atribut keamanan dalam kode dapat menambahkan perilaku ke ContractDescription yang menunjukkan di mana pernyataan kebijakan keamanan harus ditambahkan.

Mekanisme ini IPolicyExportExtension hanya mendukung pernyataan kebijakan ekspor di WSDL. Untuk mengekspor elemen WSDL kustom, Anda harus menggunakan IWsdlExportExtension mekanisme untuk memodifikasi WSDL secara langsung.

Setelah pernyataan kebijakan kustom dilampirkan ke informasi WSDL, klien dapat mendeteksi dan mengimpor pernyataan pengikatan kustom dengan menggunakan IPolicyImportExtension objek .

Metode

ExportPolicy(MetadataExporter, PolicyConversionContext)

Terapkan untuk menyertakan untuk mengekspor pernyataan kebijakan kustom tentang pengikatan.

Berlaku untuk