PolicyConversionContext Klass

Definition

Definierar en klass som används för att hämta bindningskontroller i metadata och för att bifoga implementering av bindningselement i lämpligt omfång.

public ref class PolicyConversionContext abstract
public abstract class PolicyConversionContext
type PolicyConversionContext = class
Public MustInherit Class PolicyConversionContext
Arv
PolicyConversionContext

Exempel

I följande kodexempel visas en implementering av ImportPolicy metoden som skriver alla principkontroller till konsolen. Kodkommentarna beskriver hur du hittar en specifik anpassad principkontroll, skapar och infogar ett implementeringsbindningselement och tar bort försäkran från samlingen.

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

Följande kodexempel visar hur du registrerar IPolicyImportExtension implementeringar med hjälp av konfigurationsavsnittet <policyImporters> .

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

I följande kodexempel visas hur ett anpassat bindningselement kan implementeras IPolicyExportExtension för att bifoga en anpassad principkontroll till bindningskontrollerna.

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);
    }
}

Kommentarer

En implementering av skickas PolicyConversionContext till IPolicyExportExtension och IPolicyImportExtension objekt att exportera respektive importera anpassade principkontroller till och från metadata. Vid export hämtas en samling principkontroller för att lägga till anpassade kontroller. Vid import hämtas försäkran för att importera specifika och konfigurera bindningselement på rätt sätt.

Konstruktorer

Name Description
PolicyConversionContext(ServiceEndpoint)

Initierar en ny instans av PolicyConversionContext klassen med den angivna slutpunkten.

Egenskaper

Name Description
BindingElements

Hämtar en samling bindningselement som anpassade bindningselement som implementerar principkontroller läggs till.

Contract

Hämtar kontraktet för slutpunkten.

Metoder

Name Description
Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetBindingAssertions()

Hämtar en samling principkontroller från metadata.

GetFaultBindingAssertions(FaultDescription)

Returnerar en samling principkontroller för det angivna SOAP-felet.

GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetMessageBindingAssertions(MessageDescription)

Hämtar en samling principkontroller för ett meddelande.

GetOperationBindingAssertions(OperationDescription)

Returnerar en samling principkontroller för den angivna åtgärden.

GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för