IPolicyImportExtension.ImportPolicy Metoda

Definicja

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

public:
 void ImportPolicy(System::ServiceModel::Description::MetadataImporter ^ importer, System::ServiceModel::Description::PolicyConversionContext ^ context);
public void ImportPolicy (System.ServiceModel.Description.MetadataImporter importer, System.ServiceModel.Description.PolicyConversionContext context);
abstract member ImportPolicy : System.ServiceModel.Description.MetadataImporter * System.ServiceModel.Description.PolicyConversionContext -> unit
Public Sub ImportPolicy (importer As MetadataImporter, context As PolicyConversionContext)

Parametry

importer
MetadataImporter

Używany MetadataImporter obiekt.

context
PolicyConversionContext

Zawiera PolicyConversionContext zarówno asercji zasad, które można zaimportować, jak i kolekcje elementów powiązania, do których można dodać implementujące elementy powiązania.

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 metodę w ImportPolicy celu uzyskania asercji zasad i wykonania pewnych modyfikacji zaimportowanego kontraktu lub powiązania w celu obsługi asercji. Zazwyczaj importer zasad reaguje na znalezienie niestandardowej asercji zasad, konfigurując lub wstawiając element powiązania do importowanego powiązania.

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. Modyfikuje stos powiązań lub kontrakt przez dodanie wymaganego niestandardowego BindingElements obiektu BindingElement do właściwości lub przez zmodyfikowanie PolicyConversionContext.Contract właściwości.

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.

Dotyczy