IPolicyImportExtension Interface

Defines a method for objects that import custom policy assertions about bindings.

Namespace:  System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

'Declaration
Public Interface IPolicyImportExtension

The IPolicyImportExtension type exposes the following members.

  NameDescription
Public methodImportPolicyDefines a method that can import custom policy assertions and add implementing binding elements.
Top

Implement the IPolicyImportExtension interface to search WSDL information exposed by a particular endpoint for custom policy assertions about endpoint capabilities or requirements. Typically, a policy importer searches for a specific assertion and either inserts a binding element, configures a binding element, or modifies the contract to support the requirements of the assertion.

Unlike its counterpart, IPolicyExportExtension, IPolicyImportExtension does not require implementation by a BindingElement object; you can load it using the client configuration section shown in the Examples section or programmatically by adding it to the System.ServiceModel.Description.WsdlImporter constructor.

Windows Communication Foundation (WCF) passes two objects to the ImportPolicy method, a MetadataImporter and a PolicyConversionContext. Typically the PolicyConversionContext object already contains the policy assertions for each binding scope.

An IPolicyImportExtension implementation performs the following steps:

  1. Locates the custom policy assertion for which it is responsible by calling either the GetBindingAssertions, GetMessageBindingAssertions, or GetOperationBindingAssertions methods, depending upon the scope.

  2. Removes the policy assertion from the assertion collection. The PolicyAssertionCollection.Remove method locates, returns, and removes the assertion in one step.

  3. Modify the binding stack or the contract by either adding a required custom BindingElement to the BindingElements property or by modifying the PolicyConversionContext.Contract property.

Step 2 is important. After all policy importers have been called, WCF checks for the existence of any policy assertions that remain. If one exists, WCF assumes that the policy import was unsuccessful and does not import the associated binding.

Important noteImportant

A malicious metadata supplier can attempt to send malformed XML as part of metadata in an attempt to exploit a policy importer. It is strongly recommended that custom policy importers be robust to all forms of XML that can be passed to it.

Custom MetadataImporter implementations must implement their own PolicyConversionContext object to extract the policy assertions attached to the custom metadata format.

If you want to export and import custom WSDL elements that are not policy assertions, see System.ServiceModel.Description.IWsdlExportExtension and System.ServiceModel.Description.IWsdlImportExtension.

NoteNote

You can use custom policy importers and exporters with the ServiceModel Metadata Utility Tool (Svcutil.exe) by using the same configuration elements in a configuration file and the /svcutilConfig:<configFile> option.

The following code example shows the use of the PolicyAssertionCollection.Remove method to locate, return, and remove the assertion in one step.

	#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

The following code example shows the client application configuration file to load the custom policy importer when the System.ServiceModel.Description.MetadataResolver is invoked.

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

The following code example shows the use of the MetadataResolver to download and resolve metadata into description objects.

		' Download all metadata.  
		Dim endpoints As ServiceEndpointCollection = MetadataResolver.Resolve(GetType(IStatefulService), New EndpointAddress("http://localhost:8080/StatefulService/mex"))

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft