IPolicyImportExtension.ImportPolicy Method (MetadataImporter, PolicyConversionContext)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Defines a method that can import custom policy assertions and add implementing binding elements.

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

void ImportPolicy(
	MetadataImporter importer,
	PolicyConversionContext context


Type: System.ServiceModel.Description.MetadataImporter

The MetadataImporter object in use.

Type: System.ServiceModel.Description.PolicyConversionContext

The PolicyConversionContext that contains both the policy assertions that can be imported and the collections of binding elements to which implementing binding elements can be added.

Implement the ImportPolicy method to obtain policy assertions and perform some modification of the imported contract or binding to support the assertion. Typically, a policy importer responds to finding a custom policy assertion by configuring or inserting a binding element into the binding being imported.

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

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 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)
        "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.ForegroundColor = ConsoleColor.Gray;

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

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

// Download all metadata. 
ServiceEndpointCollection endpoints
  = MetadataResolver.Resolve(
    new EndpointAddress("http://localhost:8080/StatefulService/mex")

.NET Framework
Available since 3.0
Return to top