PolicyConversionContext.GetBindingAssertions Methode

Definition

Ruft eine Auflistung von Richtlinien assertionen aus Metadaten ab.

public:
 abstract System::ServiceModel::Description::PolicyAssertionCollection ^ GetBindingAssertions();
public abstract System.ServiceModel.Description.PolicyAssertionCollection GetBindingAssertions();
abstract member GetBindingAssertions : unit -> System.ServiceModel.Description.PolicyAssertionCollection
Public MustOverride Function GetBindingAssertions () As PolicyAssertionCollection

Gibt zurück

Ein ICollection<T> Typ XmlElement , der Bindungsrichtlinien assertionen enthält.

Beispiele

Das folgende Codebeispiel zeigt eine Implementierung der ImportPolicy Methode, die alle Richtlinien assertionen in die Konsole schreibt. Die Codekommentare beschreiben, wie Sie eine bestimmte benutzerdefinierte Richtlinien assertion suchen, ein implementierende Bindungselement erstellen und einfügen und die Assertion aus der Auflistung entfernen.

public void ImportPolicy(MetadataImporter importer,
    PolicyConversionContext context)
{
    Console.WriteLine("The custom policy importer has been called.");
    foreach (XmlElement assertion in context.GetBindingAssertions())
    {
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);
        // locate a particular assertion by Name and NamespaceURI
        XmlElement customAssertion = context.GetBindingAssertions().Find(name1, ns1);
        if (customAssertion != null)
        {
          // Found assertion; remove from collection.
          context.GetBindingAssertions().Remove(customAssertion);
          Console.WriteLine(
            "Removed our custom assertion from the imported "
            + "assertions collection and inserting our custom binding element."
          );
            // Here if you find the custom policy assertion that you are looking for,
            // add the custom binding element that handles the functionality that the policy indicates.
            // Attach it to the PolicyConversionContext.BindingElements collection.
            // For example, if the custom policy had a "speed" attribute value:
            /*
              string speed
                = customAssertion.GetAttribute(SpeedBindingElement.name2, SpeedBindingElement.ns2);
              SpeedBindingElement e = new SpeedBindingElement(speed);
              context.BindingElements.Add(e);
            */
        }

        // write assertion name in red.
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);

        //write contents in gray.
        Console.WriteLine(assertion.OuterXml);
        Console.ForegroundColor = ConsoleColor.Gray;
    }
}

Das folgende Codebeispiel zeigt, wie Implementierungen mithilfe des IPolicyImportExtension Konfigurationsabschnitts registriert <policyImporters> werden.

<configuration>
  <system.serviceModel>
    <client>
      <metadata>
        <policyImporters>
          <extension type="CustomPolicyImporter, assembly"/>
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>
</configuration>

Im folgenden Codebeispiel wird veranschaulicht, wie ein benutzerdefiniertes Bindungselement implementiert IPolicyExportExtension werden kann, um eine benutzerdefinierte Richtlinien assertion an die Bindungs assertionen anzufügen.

public class MyBindingElement : BindingElement, IPolicyExportExtension
{
// BindingElement implementation . . .

    public void ExportPolicy(
     MetadataExporter exporter, PolicyConversionContext context)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement xmlElement =
               xmlDoc.CreateElement("MyPolicyAssertion");
        context.GetBindingAssertions().Add(xmlElement);
    }

    // Note: All custom binding elements must return a deep clone
    // to enable the run time to support multiple bindings using the
    // same custom binding.
    public override BindingElement Clone()
    {
        // this is just a placeholder
        return null;
    }

    // Call the inner property.
    public override T GetProperty<T>(BindingContext context)
    {
        return context.GetInnerProperty<T>();
    }
}

public class Program {
    public static void Main(string[] args) {
        EndpointAddress address =
            new EndpointAddress("http://localhost/metadata");
        CustomBinding customBinding =
            new CustomBinding(new BasicHttpBinding());
        customBinding.Elements.Add(new MyBindingElement());
        ContractDescription contract =
            ContractDescription.GetContract(typeof(MyContract));
        ServiceEndpoint endpoint =
            new ServiceEndpoint(contract, customBinding, address);
        MetadataExporter exporter = new WsdlExporter();
        exporter.ExportEndpoint(endpoint);
    }
}

Hinweise

Die zurückgegebene GetBindingAssertions Auflistung ist keine Kopie. Sie können bei Bedarf Assertionen hinzufügen und entfernen.

Gilt für: