Freigeben über


IPolicyImportExtension.ImportPolicy Methode

Definition

Definiert eine Methode, mit der benutzerdefinierte Richtlinienassertionen importiert und implementierende Bindungselemente hinzugefügt werden können.

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)

Parameter

importer
MetadataImporter

Das verwendete MetadataImporter-Objekt.

context
PolicyConversionContext

Das PolicyConversionContext-Objekt, das sowohl die Richtlinienassertionen enthält, die importiert werden können, als auch die Auflistungen von Bindungselementen, denen Implementierungsbindungselemente hinzugefügt werden können.

Beispiele

Im folgenden Codebeispiel wird die Verwendung der PolicyAssertionCollection.Remove-Methode zum Suchen, Zurückgeben und Entfernen der Assertion in einem Schritt gezeigt.

  #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

Im folgenden Codebeispiel wird die Clientanwendungs-Konfigurationsdatei gezeigt, mit der das benutzerdefinierte Richtlinienimportprogramm geladen wird, wenn System.ServiceModel.Description.MetadataResolver aufgerufen wird.

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

Im folgenden Codebeispiel wird die Verwendung von MetadataResolver gezeigt, mit dem Metadaten in Beschreibungsobjekte heruntergeladen und aufgelöst werden.

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

Hinweise

Implementiert die ImportPolicy-Methode, mit der Richtlinienassertionen abgerufen und einige Änderungen am importierten Vertrag oder der importierten Bindung zur Unterstützung der Assertion durchgeführt werden. In der Regel reagiert ein Richtlinienimportprogramm auf das Finden einer benutzerdefinierten Richtlinienassertion durch Konfigurieren oder Einfügen eines Bindungselements in die zu importierende Bindung.

Windows Communication Foundation (WCF) übergibt zwei Objekte an die ImportPolicy Methode, a MetadataImporter und aPolicyConversionContext. In der Regel enthält das PolicyConversionContext-Objekt bereits die Richtlinienassertionen für jeden Bindungsbereich.

Eine IPolicyImportExtension-Implementierung führt die folgenden Schritte aus:

  1. Sucht die benutzerdefinierte Richtlinienassertion, für die sie verantwortlich ist, durch Aufruf der Methode GetBindingAssertions, GetMessageBindingAssertions oder GetOperationBindingAssertions (je nach Bereich).

  2. Entfernt die Richtlinienassertion aus der Assertionsauflistung. Die PolicyAssertionCollection.Remove-Methode sucht die Assertion, gibt sie zurück und entfernt sie (in einem Schritt).

  3. Ändert den Bindungsstapel oder den Vertrag, indem entweder ein erforderliches benutzerdefiniertes BindingElement der BindingElements-Eigenschaft hinzugefügt oder die PolicyConversionContext.Contract-Eigenschaft geändert wird.

Schritt 2 ist wichtig. Nachdem alle Richtlinienimporteure aufgerufen wurden, überprüft WCF nach der Existenz von Richtlinienbehauptungen, die weiterhin bestehen. Wenn eine vorhanden ist, wird WCF davon ausgegangen, dass der Richtlinienimport nicht erfolgreich war und die zugehörige Bindung nicht importiert.

Gilt für