Condividi tramite


IPolicyExportExtension Interfaccia

Definizione

Implementare IPolicyExportExtension per inserire asserzioni di criteri di associazione personalizzate nelle informazioni WSDL (Web Services Description Language).

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

Esempio

Nell'esempio di codice seguente viene illustrata l'implementazione di IPolicyExportExtension in un oggetto BindingElement. In questo esempio, un elemento di associazione personalizzato viene collegato al file WSDL a livello di associazione.

#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

Nell'esempio di codice seguente viene illustrata un'implementazione System.ServiceModel.Configuration.BindingElementExtensionElement che consente di caricare l'utilità di esportazione dei criteri precedente da un file di configurazione dell'applicazione.

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

Nell'esempio seguente viene illustrato il file di configurazione host che carica l'utilità di esportazione di criteri personalizzata.

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

Nell'esempio seguente viene illustrata l'asserzione personalizzata nel 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="" />

Commenti

Implementare l'interfaccia IPolicyExportExtension in un System.ServiceModel.Channels.BindingElement oggetto per scrivere istruzioni sulle funzionalità o sui requisiti degli endpoint nelle informazioni WSDL esposte da un determinato endpoint. In genere l'elemento di associazione è uno che implementa alcune funzionalità, ma questo non è obbligatorio. Per caricare l'utilità di esportazione dei criteri da un file di configurazione, implementare un System.ServiceModel.Configuration.BindingElementExtensionElement oggetto che restituisce l'oggetto di esportazione dei criteri BindingElement .

L'utilità di esportazione dei criteri viene usata da Windows Communication Foundation (WCF) per usare asserzioni di criteri per comunicare con i client l'esistenza di tale requisito di associazione o funzionalità endpoint personalizzata.

Il ExportPolicy metodo accetta gli MetadataExporter oggetti e PolicyConversionContext . Usare i GetBindingAssertionsmetodi , GetMessageBindingAssertionse GetOperationBindingAssertions per ottenere raccolte di asserzioni di criteri che sono già state esportate in vari ambiti. Aggiungere quindi l'oggetto asserzione di criteri personalizzata alla raccolta appropriata.

La Contract proprietà espone l'oggetto ContractDescription per l'endpoint da esportare. Ciò consente all'estensione di definire correttamente l'ambito IPolicyExportExtension delle asserzioni dei criteri esportate. Ad esempio, gli attributi di sicurezza nel codice possono aggiungere comportamenti a ContractDescription che indicano dove aggiungere asserzioni di criteri di sicurezza.

Il IPolicyExportExtension meccanismo supporta solo l'esportazione di asserzioni di criteri in WSDL. Per esportare elementi WSDL personalizzati, è necessario usare il IWsdlExportExtension meccanismo per modificare direttamente il file WSDL.

Dopo che le asserzioni di criteri personalizzate sono state collegate alle informazioni WSDL, i client possono rilevare e importare le asserzioni di associazione personalizzate usando un IPolicyImportExtension oggetto .

Metodi

Nome Descrizione
ExportPolicy(MetadataExporter, PolicyConversionContext)

Implementare per includere per l'esportazione di un'asserzione di criteri personalizzata sulle associazioni.

Si applica a