IPolicyImportExtension.ImportPolicy Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Definierar en metod som kan importera anpassade principkontroller och lägga till implementering av bindningselement.
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)
Parametrar
- importer
- MetadataImporter
Objektet MetadataImporter som används.
- context
- PolicyConversionContext
Som PolicyConversionContext innehåller både de principkontroller som kan importeras och de samlingar av bindningselement som implementerar bindningselement kan läggas till.
Exempel
I följande kodexempel visas hur metoden används PolicyAssertionCollection.Remove för att hitta, returnera och ta bort försäkran i ett steg.
#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
I följande kodexempel visas konfigurationsfilen för klientprogrammet för att läsa in den anpassade principimportören när den System.ServiceModel.Description.MetadataResolver anropas.
<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>
I följande kodexempel visas användningen av MetadataResolver för att ladda ned och matcha metadata till beskrivningsobjekt.
// 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"))
Kommentarer
ImportPolicy Implementera metoden för att hämta principkontroller och utföra vissa ändringar av det importerade kontraktet eller bindningen för att stödja försäkran. Vanligtvis svarar en principimportör på att hitta en anpassad principkontroll genom att konfigurera eller infoga ett bindningselement i bindningen som importeras.
Windows Communication Foundation (WCF) skickar två objekt till metoden ImportPolicy, en MetadataImporter och en PolicyConversionContext. PolicyConversionContext Vanligtvis innehåller objektet redan principkontroller för varje bindningsomfång.
En IPolicyImportExtension implementering utför följande steg:
Letar upp den anpassade principkontroll som den ansvarar för genom att anropa antingen GetBindingAssertionsmetoderna , GetMessageBindingAssertionseller GetOperationBindingAssertions beroende på omfånget.
Tar bort princippåståendet från kontrollsamlingen. Metoden PolicyAssertionCollection.Remove letar upp, returnerar och tar bort försäkran i ett steg.
Ändrar bindningsstacken eller kontraktet genom att antingen lägga till en obligatorisk anpassad till BindingElementBindingElements egenskapen eller genom att PolicyConversionContext.Contract ändra egenskapen.
Steg 2 är viktigt. Efter att alla politiska importörer har kallats in kontrollerar WCF att det finns några politiska intyg som kvarstår. Om det finns en sådan förutsätter WCF att principimporten misslyckades och inte importerar den associerade bindningen.