IPolicyImportExtension.ImportPolicy Methode
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 waarmee aangepaste beleidsverklaringen kunnen worden geïmporteerd en bindingselementen kunnen worden geïmplementeerd.
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)
Parameters
- importer
- MetadataImporter
Het MetadataImporter object dat wordt gebruikt.
- context
- PolicyConversionContext
De PolicyConversionContext verzamelingen die zowel de beleidsverklaringen bevatten die kunnen worden geïmporteerd als de verzamelingen bindingselementen waaraan bindingselementen kunnen worden geïmplementeerd, kunnen worden toegevoegd.
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 methode voor het ImportPolicy verkrijgen van beleidsverklaringen en voer een wijziging van het geïmporteerde contract of de binding uit om de assertie te ondersteunen. Normaal gesproken reageert een beleidsimporteur op het vinden van een aangepaste beleidsverklaring door een bindingselement te configureren of in te voegen in de binding die wordt geïmporteerd.
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.
Wijzigt de bindingsstack of het contract door een vereiste aangepaste eigenschap BindingElement toe te BindingElements 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.