Edit

Share via


How to: Export Custom Policy Assertions

Policy assertions describe the capabilities and requirements of a service endpoint. Service applications can use custom policy assertions in service metadata to communicate endpoint, binding or contract customization information to the client application. You can use Windows Communication Foundation (WCF) to export assertions in policy expressions attached in WSDL bindings at the endpoint, operation, or message subjects, depending upon the capabilities or requirements you are communicating.

Custom policy assertions are exported by implementing the System.ServiceModel.Description.IPolicyExportExtension interface on a System.ServiceModel.Channels.BindingElement and either inserting the binding element directly into the binding of the service endpoint or by registering the binding element in your application configuration file. Your policy export implementation should add your custom policy assertion as a System.Xml.XmlElement instance to the appropriate System.ServiceModel.Description.PolicyAssertionCollection on the System.ServiceModel.Description.PolicyConversionContext passed into the ExportPolicy method.

In addition you must check the PolicyVersion property of the WsdlExporter class and export nested policy expressions and policy framework attributes in the correct namespace based on the policy version specified.

To import custom policy assertions, see System.ServiceModel.Description.IPolicyImportExtension and How to: Import Custom Policy Assertions.

To export custom policy assertions

  1. Implement the System.ServiceModel.Description.IPolicyExportExtension interface on a System.ServiceModel.Channels.BindingElement. The following code example shows the implementation of a custom policy assertion at the binding level.

    #region IPolicyExporter Members
    public void ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
    {
      if (exporter == null)
        throw new NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.");
      if (policyContext == null)
        throw new NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.");
    
      XmlElement elem = doc.CreateElement(name1, ns1);
      elem.InnerText = "My custom text.";
      XmlAttribute att = doc.CreateAttribute("MyCustomAttribute", ns1);
      att.Value = "ExampleValue";
      elem.Attributes.Append(att);
      XmlElement subElement = doc.CreateElement("MyCustomSubElement", ns1);
      subElement.InnerText = "Custom Subelement Text.";
      elem.AppendChild(subElement);
      policyContext.GetBindingAssertions().Add(elem);
      Console.WriteLine("The custom policy exporter was called.");
    }
    #endregion
    
    #Region "IPolicyExporter Members"
            Public Sub ExportPolicy(ByVal exporter As MetadataExporter, ByVal policyContext As PolicyConversionContext) Implements IPolicyExportExtension.ExportPolicy
                If exporter Is Nothing Then
                    Throw New NullReferenceException("The MetadataExporter object passed to the ExporterBindingElement is null.")
                End If
                If policyContext Is Nothing Then
                    Throw New NullReferenceException("The PolicyConversionContext object passed to the ExporterBindingElement is null.")
                End If
    
                Dim elem As XmlElement = doc.CreateElement(name1, ns1)
                elem.InnerText = "My custom text."
                Dim att As XmlAttribute = doc.CreateAttribute("MyCustomAttribute", ns1)
                att.Value = "ExampleValue"
                elem.Attributes.Append(att)
                Dim subElement As XmlElement = doc.CreateElement("MyCustomSubElement", ns1)
                subElement.InnerText = "Custom Subelement Text."
                elem.AppendChild(subElement)
                policyContext.GetBindingAssertions().Add(elem)
                Console.WriteLine("The custom policy exporter was called.")
            End Sub
    #End Region
    
  2. Insert the binding element into the endpoint binding either programmatically or using an application configuration file. See the following procedures.

To insert a binding element using an application configuration file

  1. Implement System.ServiceModel.Configuration.BindingElementExtensionElement for your custom policy assertion binding element.

  2. Add the binding element extension to the configuration file using the <bindingElementExtensions> element.

  3. Build a custom binding using the System.ServiceModel.Channels.CustomBinding.

To insert a binding element programmatically

  1. Create a new System.ServiceModel.Channels.BindingElement and add it to a System.ServiceModel.Channels.CustomBinding.

  2. Add the custom binding from step 1. to a new endpoint and add that new service endpoint to the System.ServiceModel.ServiceHost by calling the AddServiceEndpoint method.

  3. Open the ServiceHost. The following code example shows the creation of a custom binding and the programmatic insertion of binding elements.

    Uri baseAddress = new Uri("http://localhost:8000/servicemodelsamples/service");
    
    // Create a ServiceHost for the CalculatorService type and provide the base address.
    using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
    {
        // Create a custom binding that contains two binding elements.
        ReliableSessionBindingElement reliableSession = new ReliableSessionBindingElement();
        reliableSession.Ordered = true;
    
        HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
        httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
        httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    
        CustomBinding binding = new CustomBinding(reliableSession, httpTransport);
    
        // Add an endpoint using that binding.
        serviceHost.AddServiceEndpoint(typeof(ICalculator), binding, "");
    
        // Add a MEX endpoint.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        smb.HttpGetUrl = new Uri("http://localhost:8001/servicemodelsamples");
        serviceHost.Description.Behaviors.Add(smb);
    
        // Open the ServiceHostBase to create listeners and start listening for messages.
        serviceHost.Open();
    
        // The service can now be accessed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine();
        Console.ReadLine();
    
        // Close the ServiceHostBase to shutdown the service.
        serviceHost.Close();
    }
    
    Dim baseAddress As New Uri("http://localhost:8000/servicemodelsamples/service")
    
    ' Create a ServiceHost for the CalculatorService type and provide the base address.
    Using serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
        ' Create a custom binding that contains two binding elements.
        Dim reliableSession As New ReliableSessionBindingElement()
        reliableSession.Ordered = True
    
        Dim httpTransport As New HttpTransportBindingElement()
        httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous
        httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard
    
        Dim binding As New CustomBinding(reliableSession, httpTransport)
    
        ' Add an endpoint using that binding.
        serviceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")
    
        ' Add a MEX endpoint.
        Dim smb As New ServiceMetadataBehavior()
        smb.HttpGetEnabled = True
        smb.HttpGetUrl = New Uri("http://localhost:8001/servicemodelsamples")
        serviceHost.Description.Behaviors.Add(smb)
    
        ' Open the ServiceHostBase to create listeners and start listening for messages.
        serviceHost.Open()
    
        ' The service can now be accessed.
        Console.WriteLine("The service is ready.")
        Console.WriteLine("Press <ENTER> to terminate service.")
        Console.WriteLine()
        Console.ReadLine()
    
        ' Close the ServiceHostBase to shutdown the service.
        serviceHost.Close()
    End Using
    

See also