Export (0) Print
Expand All

PolicyConversionContext Class

Defines a class used to retrieve binding assertions in metadata and to attach implementing binding elements at the appropriate scope.

System.Object
  System.ServiceModel.Description.PolicyConversionContext

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

public abstract class PolicyConversionContext

The PolicyConversionContext type exposes the following members.

  NameDescription
Protected methodPolicyConversionContextInitializes a new instance of the PolicyConversionContext class using the specified endpoint.
Top

  NameDescription
Public propertyBindingElementsGets a collection of binding elements to which custom binding elements that implement policy assertions are added.
Public propertyContractGets the contract for the endpoint.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetBindingAssertionsGets a collection of policy assertions from metadata.
Public methodGetFaultBindingAssertionsReturns a collection of policy assertions for the specified SOAP fault.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetMessageBindingAssertionsGets a collection of policy assertions for a message.
Public methodGetOperationBindingAssertionsReturns a collection of policy assertions for the specified operation.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

An implementation of the PolicyConversionContext is passed to IPolicyExportExtension and IPolicyImportExtension objects to export and import, respectively, custom policy assertions to and from metadata. On export, a collection of policy assertions are retrieved in order to add custom assertions. On import, the assertions are retrieved in order to import specific ones and configure binding elements appropriately.

The following code example shows an implementation of the ImportPolicy method that writes all policy assertions to the console. The code comments describe how to locate a specific custom policy assertion, create and insert an implementing binding element, and remove the assertion from the collection.

public void ImportPolicy(MetadataImporter importer, 
    PolicyConversionContext context)
{
    Console.WriteLine("The custom policy importer has been called.");
    foreach (XmlElement assertion in context.GetBindingAssertions())
    {
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);
        // locate a particular assertion by Name and NamespaceURI
        XmlElement customAssertion = context.GetBindingAssertions().Find(name1, ns1);
        if (customAssertion != null)
        {
          // Found assertion; remove from collection.
          context.GetBindingAssertions().Remove(customAssertion);
          Console.WriteLine(
            "Removed our custom assertion from the imported "
            + "assertions collection and inserting our custom binding element."
          );
            // Here if you find the custom policy assertion that you are looking for, 
            // add the custom binding element that handles the functionality that the policy indicates. 
            // Attach it to the PolicyConversionContext.BindingElements collection. 
            // For example, if the custom policy had a "speed" attribute value:
            /*
              string speed 
                = customAssertion.GetAttribute(SpeedBindingElement.name2, SpeedBindingElement.ns2);
              SpeedBindingElement e = new SpeedBindingElement(speed);
              context.BindingElements.Add(e);
            */
        }

        // write assertion name in red.
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);

        //write contents in gray.
        Console.WriteLine(assertion.OuterXml);
        Console.ForegroundColor = ConsoleColor.Gray; 
    }
}
  public void ImportPolicy(MetadataImporter importer, 
      PolicyConversionContext context)
  {
      Console.WriteLine("The custom policy importer has been called.");
      foreach (XmlElement assertion in context.GetBindingAssertions())
      {
          // locate a particular assertion by Name and NamespaceURI
          if ((assertion.Name == "name") && 
              assertion.NamespaceURI == "http://localhost/name/uri")
          {
              // write assertion name in red.
              Console.ForegroundColor = ConsoleColor.Red;
              Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);

              //write contents in gray.
              Console.WriteLine(assertion.OuterXml);
              Console.ForegroundColor = ConsoleColor.Gray; 

              // Here if you find the custom policy assertion that you are looking for,
              // add the custom binding element that handles the functionality that the policy indicates
              // Attach it to the PolicyConversionContext.BindingElements collection.
              // For example, if the custom policy had a "speed" attribute value:
              /*
                string speed = assertion.GetAttribute(SpeedBindingElement.name2, SpeedBindingElement.ns2);
                SpeedBindingElement e = new SpeedBindingElement(speed);
                context.BindingElements.Add(e);
                // remove your custom assertion from the assertions collection.
                context.GetBindingAssertions()Remove(assertion);
              */
              continue;
          }
      Console.WriteLine(assertion.NamespaceURI + " : " + assertion.Name);
  }
}

The following code example shows how to register IPolicyImportExtension implementations using the <policyImporters> configuration section.

<configuration>
  <system.serviceModel>
    <client>
      <metadata>
        <policyImporters>
          <extension type="CustomPolicyImporter, assembly"/>
        </policyImporters>
      </metadata>
    </client>
  </system.serviceModel>
</configuration>

The following code example demonstrates how a custom binding element can implement IPolicyExportExtension to attach a custom policy assertion to the binding assertions.

	public class MyBindingElement : BindingElement, IPolicyExportExtension
	{
	// BindingElement implementation . . . 

		public void ExportPolicy(
		 MetadataExporter exporter, PolicyConversionContext context)
		{
			XmlDocument xmlDoc = new XmlDocument();
			XmlElement xmlElement = 
			       xmlDoc.CreateElement("MyPolicyAssertion");
			context.GetBindingAssertions().Add(xmlElement);
		}


		// Note: All custom binding elements must return a deep clone  
		// to enable the run time to support multiple bindings using the  
		// same custom binding. 
		public override BindingElement Clone()
		{
			// this is just a placeholder 
			return null;
		}


		// Call the inner property. 
		public override T GetProperty<T>(BindingContext context)
		{
			return context.GetInnerProperty<T>();
		}

	}

	public class Program {
		public static void Main(string[] args) {
			EndpointAddress address = 
				new EndpointAddress("http://localhost/metadata");
			CustomBinding customBinding = 
				new CustomBinding(new BasicHttpBinding());
			customBinding.Elements.Add(new MyBindingElement());
			ContractDescription contract =
				ContractDescription.GetContract(typeof(MyContract));
			ServiceEndpoint endpoint = 
				new ServiceEndpoint(contract, customBinding, address);
			MetadataExporter exporter = new WsdlExporter();
			exporter.ExportEndpoint(endpoint);
		}
	}
	public class MyBindingElement : BindingElement, IPolicyExportExtension
	{
	// BindingElement implementation . . .

		public void ExportPolicy(
		 MetadataExporter exporter, PolicyConversionContext context)
		{
			XmlDocument xmlDoc = new XmlDocument();
			XmlElement xmlElement = 
			       xmlDoc.CreateElement("MyPolicyAssertion");
			context.GetBindingAssertions().Add(xmlElement);
		}


		// Note: All custom binding elements must return a deep clone 
		// to enable the run time to support multiple bindings using the 
		// same custom binding.
		public override BindingElement Clone()
		{
			// this is just a placeholder
			return this;
		}


		// Call the inner property.
		public override T GetProperty<T>(BindingContext context)
		{
			return context.GetInnerProperty<T>();
		}

	}

	public class Program {
		public static void Main(string[] args) {
			EndpointAddress address = 
				new EndpointAddress("http://localhost/metadata");
			CustomBinding customBinding = 
				new CustomBinding(new BasicHttpBinding());
			customBinding.Elements.Add(new MyBindingElement());
			ContractDescription contract =
                ContractDescription.GetContract(
                    typeof(StatefulServiceClient));
			ServiceEndpoint endpoint =
                new ServiceEndpoint(contract, new WSHttpBinding(), address);

			MetadataExporter exporter = new WsdlExporter();
			exporter.ExportEndpoint(endpoint);
		}
	}

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft