IPolicyImportExtension Gränssnitt

Definition

Definierar en metod för objekt som importerar anpassade principkontroller om bindningar.

public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
Härledda

Exempel

I följande kodexempel visas hur metoden används PolicyAssertionCollection.Remove för att hitta, returnera och ta bort försäkran i ett steg.

  #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

I följande kodexempel visas konfigurationsfilen för klientprogrammet för att läsa in den anpassade principimportören när den System.ServiceModel.Description.MetadataResolver anropas.

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

I följande kodexempel visas användningen av MetadataResolver för att ladda ned och matcha metadata till beskrivningsobjekt.

// 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"))

Kommentarer

IPolicyImportExtension Implementera gränssnittet för att söka efter WSDL-information som exponeras av en viss slutpunkt efter anpassade principkontroller om slutpunktsfunktioner eller krav. Vanligtvis söker en principimportör efter en specifik försäkran och infogar antingen ett bindningselement, konfigurerar ett bindningselement eller ändrar kontraktet för att stödja kraven i försäkran.

Till skillnad från dess motsvarighet IPolicyExportExtensionkräver , IPolicyImportExtension inte implementering av ett BindingElement objekt. Du kan läsa in det med hjälp av klientkonfigurationsavsnittet som visas i avsnittet Exempel eller programmatiskt genom att lägga till det i System.ServiceModel.Description.WsdlImporter konstruktorn.

Windows Communication Foundation (WCF) skickar två objekt till metoden ImportPolicy, en MetadataImporter och en PolicyConversionContext. PolicyConversionContext Vanligtvis innehåller objektet redan principkontroller för varje bindningsomfång.

En IPolicyImportExtension implementering utför följande steg:

  1. Letar upp den anpassade principkontroll som den ansvarar för genom att anropa antingen GetBindingAssertionsmetoderna , GetMessageBindingAssertionseller GetOperationBindingAssertions beroende på omfånget.

  2. Tar bort princippåståendet från kontrollsamlingen. Metoden PolicyAssertionCollection.Remove letar upp, returnerar och tar bort försäkran i ett steg.

  3. Ändra bindningsstacken eller kontraktet genom att antingen lägga till en obligatorisk anpassad till BindingElementBindingElements egenskapen eller genom att PolicyConversionContext.Contract ändra egenskapen.

Steg 2 är viktigt. Efter att alla politiska importörer har kallats in kontrollerar WCF att det finns några politiska intyg som kvarstår. Om det finns en sådan förutsätter WCF att principimporten misslyckades och inte importerar den associerade bindningen.

Important

En leverantör av skadliga metadata kan försöka skicka felaktig XML som en del av metadata i ett försök att utnyttja en principimportör. Vi rekommenderar starkt att anpassade principimportörer är robusta för alla former av XML som kan skickas till den.

Anpassade MetadataImporter implementeringar måste implementera sitt eget PolicyConversionContext objekt för att extrahera de principkontroller som är kopplade till det anpassade metadataformatet.

Om du vill exportera och importera anpassade WSDL-element som inte är principkontroller läser System.ServiceModel.Description.IWsdlExportExtension du och System.ServiceModel.Description.IWsdlImportExtension.

Anmärkning

Du kan använda anpassade principimportörer och exportörer med Verktyget för ServiceModel-metadata (Svcutil.exe) med hjälp av samma konfigurationselement i en konfigurationsfil och /svcutilConfig:<configFile> alternativet .

Metoder

Name Description
ImportPolicy(MetadataImporter, PolicyConversionContext)

Definierar en metod som kan importera anpassade principkontroller och lägga till implementering av bindningselement.

Gäller för