IPolicyImportExtension Interface
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Definieert een methode voor objecten die aangepaste beleidsverklaringen over bindingen importeren.
public interface class IPolicyImportExtension
public interface IPolicyImportExtension
type IPolicyImportExtension = interface
Public Interface IPolicyImportExtension
- Afgeleid
Voorbeelden
In het volgende codevoorbeeld ziet u het gebruik van de PolicyAssertionCollection.Remove methode om de assertie in één stap te zoeken, te retourneren en te verwijderen.
#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
In het volgende codevoorbeeld ziet u het configuratiebestand van de clienttoepassing om de importfunctie voor aangepast beleid te laden wanneer de System.ServiceModel.Description.MetadataResolver clienttoepassing wordt aangeroepen.
<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>
In het volgende codevoorbeeld ziet u hoe MetadataResolver u metagegevens kunt downloaden en omzetten in beschrijvingsobjecten.
// 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"))
Opmerkingen
Implementeer de IPolicyImportExtension interface om WSDL-informatie te doorzoeken die door een bepaald eindpunt wordt weergegeven voor aangepaste beleidsverklaringen over eindpuntmogelijkheden of -vereisten. Normaal gesproken zoekt een beleidsimporteur naar een specifieke assertie en voegt een bindingselement in, configureert een bindingselement of wijzigt het contract om de vereisten van de assertie te ondersteunen.
In tegenstelling tot de tegenhanger, IPolicyExportExtensionIPolicyImportExtension is geen implementatie van een BindingElement object vereist. U kunt het laden met behulp van de sectie Clientconfiguratie die wordt weergegeven in de sectie Voorbeelden of programmatisch door het toe te voegen aan de System.ServiceModel.Description.WsdlImporter constructor.
Windows Communication Foundation (WCF) geeft twee objecten door aan de methode ImportPolicy, een MetadataImporter en een PolicyConversionContext. Doorgaans bevat het PolicyConversionContext object al de beleidsverklaringen voor elk bindingsbereik.
Een IPolicyImportExtension implementatie voert de volgende stappen uit:
Zoekt de aangepaste beleidsverklaring waarvoor het verantwoordelijk is door de GetBindingAssertions, GetMessageBindingAssertionsof GetOperationBindingAssertions methoden aan te roepen, afhankelijk van het bereik.
Hiermee verwijdert u de beleidsverklaring uit de assertieverzameling. De PolicyAssertionCollection.Remove methode zoekt, retourneert en verwijdert de assertie in één stap.
Wijzig de bindingsstack of het contract door een vereiste aangepaste BindingElement aan de BindingElements eigenschap toe te voegen of door de PolicyConversionContext.Contract eigenschap te wijzigen.
Stap 2 is belangrijk. Nadat alle beleidsimporteurs zijn aangeroepen, controleert WCF op het bestaan van beleidsverklaringen die blijven bestaan. Als er een bestaat, wordt ervan uitgegaan dat het importeren van het beleid is mislukt en dat de bijbehorende binding niet wordt geïmporteerd.
Important
Een kwaadwillende leverancier van metagegevens kan proberen onjuiste XML te verzenden als onderdeel van metagegevens in een poging om een beleidsimporteur te exploiteren. Het wordt sterk aanbevolen dat aangepaste beleidsimporteurs robuust zijn voor alle vormen van XML die eraan kunnen worden doorgegeven.
Aangepaste MetadataImporter implementaties moeten hun eigen PolicyConversionContext object implementeren om de beleidsverklaringen te extraheren die zijn gekoppeld aan de aangepaste metagegevensindeling.
Als u aangepaste WSDL-elementen wilt exporteren en importeren die geen beleidsverklaringen zijn, raadpleegt System.ServiceModel.Description.IWsdlExportExtension u en System.ServiceModel.Description.IWsdlImportExtension.
Note
U kunt aangepaste beleidsimporteurs en exporteurs gebruiken met het hulpprogramma voor servicemodelmetagegevens (Svcutil.exe) met behulp van dezelfde configuratie-elementen in een configuratiebestand en de /svcutilConfig:<configFile> optie.
Methoden
| Name | Description |
|---|---|
| ImportPolicy(MetadataImporter, PolicyConversionContext) |
Definieert een methode waarmee aangepaste beleidsverklaringen kunnen worden geïmporteerd en bindingselementen kunnen worden geïmplementeerd. |