IPolicyImportExtension Interfejs

Definicja

Definiuje metodę dla obiektów importujących niestandardowe asercji zasad dotyczących powiązań.

public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
Pochodne

Przykłady

Poniższy przykład kodu przedstawia użycie PolicyAssertionCollection.Remove metody do lokalizowania, zwracania i usuwania asercji w jednym kroku.

  #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

Poniższy przykład kodu przedstawia plik konfiguracji aplikacji klienckiej w celu załadowania niestandardowego importera System.ServiceModel.Description.MetadataResolver zasad po wywołaniu.

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

Poniższy przykład kodu przedstawia użycie elementu MetadataResolver do pobierania i rozpoznawania metadanych w obiektach opisu.

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

Uwagi

Zaimplementuj interfejs do IPolicyImportExtension wyszukiwania informacji WSDL uwidocznionych przez określony punkt końcowy dla niestandardowych asercji zasad dotyczących możliwości lub wymagań punktu końcowego. Zazwyczaj importer zasad wyszukuje określone potwierdzenie i wstawia element powiązania, konfiguruje element powiązania lub modyfikuje kontrakt w celu spełnienia wymagań asercji.

W przeciwieństwie do jego odpowiednika , IPolicyExportExtensionIPolicyImportExtension nie wymaga implementacji przez BindingElement obiekt. Można go załadować przy użyciu sekcji konfiguracji klienta pokazanej w sekcji Przykłady lub programowo, dodając go do konstruktoraSystem.ServiceModel.Description.WsdlImporter.

Windows Communication Foundation (WCF) przekazuje dwa obiekty do ImportPolicy metody , a MetadataImporter i PolicyConversionContext. PolicyConversionContext Zazwyczaj obiekt zawiera już asercji zasad dla każdego zakresu powiązania.

Implementacja IPolicyImportExtension wykonuje następujące kroki:

  1. Lokalizuje niestandardową asercję zasad, dla której jest odpowiedzialna, wywołując metody GetBindingAssertions, lub GetOperationBindingAssertions w GetMessageBindingAssertionszależności od zakresu.

  2. Usuwa asercji zasad z kolekcji asercji. Metoda PolicyAssertionCollection.Remove lokalizuje, zwraca i usuwa asercji w jednym kroku.

  3. Zmodyfikuj stos powiązań lub kontrakt, dodając wymagany niestandardowy element niestandardowy BindingElement do BindingElements właściwości lub modyfikując PolicyConversionContext.Contract właściwość .

Krok 2 jest ważny. Po wywołaniu wszystkich importerów zasad program WCF sprawdza istnienie wszelkich asercji zasad, które pozostają. Jeśli istnieje, WCF zakłada, że importowanie zasad nie powiodło się i nie importuje skojarzonego powiązania.

Ważne

Złośliwy dostawca metadanych może próbować wysłać źle sformułowany kod XML jako część metadanych w celu wykorzystania importera zasad. Zdecydowanie zaleca się, aby importerzy zasad niestandardowych mogli być niezawodni dla wszystkich form kodu XML, które można do niego przekazać.

Implementacje niestandardowe MetadataImporter muszą implementować własny PolicyConversionContext obiekt w celu wyodrębnienia asercji zasad dołączonych do niestandardowego formatu metadanych.

Jeśli chcesz wyeksportować i zaimportować niestandardowe elementy WSDL, które nie są asercją zasad, zobacz System.ServiceModel.Description.IWsdlExportExtension i System.ServiceModel.Description.IWsdlImportExtension.

Uwaga

Możesz użyć niestandardowych importerów zasad i eksporterów z narzędziem ServiceModel Metadata Tool (Svcutil.exe), używając tych samych elementów konfiguracji w pliku konfiguracji i /svcutilConfig:<configFile> opcji.

Metody

ImportPolicy(MetadataImporter, PolicyConversionContext)

Definiuje metodę, która może importować niestandardowe asercji zasad i dodawać implementujące elementy powiązania.

Dotyczy