IPolicyExportExtension.ExportPolicy Method
Implement to include for exporting a custom policy assertion about bindings.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Parameters
- exporter
- Type: System.ServiceModel.Description.MetadataExporter
The MetadataExporter that you can use to modify the exporting process.
- context
- Type: System.ServiceModel.Description.PolicyConversionContext
The PolicyConversionContext that you can use to insert your custom policy assertion.
Use the GetBindingAssertions, GetMessageBindingAssertions, and GetOperationBindingAssertions methods to obtain collections of policy assertions that have already been exported at various scopes. Then add your IPolicyExportExtension object to the appropriate collection.
The following code example shows the implementation of IPolicyExportExtension on a BindingElement. In this example, a custom binding element is attached to the WSDL file 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
The following code example shows a System.ServiceModel.Configuration.BindingElementExtensionElement implementation that enables the preceding policy exporter to be loaded from an application configuration file.
public class ExporterBindingElementConfigurationSection : BindingElementExtensionElement { public ExporterBindingElementConfigurationSection() { Console.WriteLine("Exporter configuration section created."); } public override Type BindingElementType { get { return typeof(ExporterBindingElement); } } protected override BindingElement CreateBindingElement() { return new ExporterBindingElement(); } }
The following example shows the host configuration file that loads the custom policy exporter.
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.StatefulService"
behaviorConfiguration="addMetadata"
>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/StatefulService"/>
</baseAddresses>
</host>
<endpoint
address="http://localhost:8080/StatefulService"
binding="customBinding"
bindingConfiguration="exporter"
contract="Microsoft.WCF.Documentation.IStatefulService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="addMetadata">
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<!--
Use the name attribute of the binding element as
the value of the bindingConfiguration attribute in
your endpoint.
-->
<binding name ="exporter">
<!-- Use the name attribute of your binding element extension specified below. -->
<!-- Be certain the order of your custom binding elements is correct. -->
<exporterBinding />
<reliableSession/>
<textMessageEncoding messageVersion="Default" />
<httpTransport/>
</binding>
</customBinding>
</bindings>
<extensions>
<bindingElementExtensions>
<!-- Use the add element to associate your bindingelement configuration handler and give it a name to use. -->
<add
type="Microsoft.WCF.Documentation.ExporterBindingElementConfigurationSection,PolicyExtensions"
name="exporterBinding" />
</bindingElementExtensions>
</extensions>
</system.serviceModel>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.StatefulService"
behaviorConfiguration="addMetadata"
>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/StatefulService"/>
</baseAddresses>
</host>
<endpoint
address="http://localhost:8080/StatefulService"
binding="customBinding"
bindingConfiguration="exporter"
contract="Microsoft.WCF.Documentation.IStatefulService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="addMetadata">
<serviceMetadata
httpGetEnabled="true"
httpGetUrl=""
/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<!--
Use the name attribute of the binding element as
the value of the bindingConfiguration attribute in
your endpoint.
-->
<binding name ="exporter">
<!-- Use the name attribute of your binding element extension specified below. -->
<!-- Be certain the order of your custom binding elements is correct. -->
<exporterBinding />
<reliableSession/>
<textMessageEncoding messageVersion="Default" />
<httpTransport/>
</binding>
</customBinding>
</bindings>
<extensions>
<bindingElementExtensions>
<!-- Use the add element to associate your bindingelement configuration handler and give it a name to use. -->
<add
type="Microsoft.WCF.Documentation.ExporterBindingElementConfigurationSection,PolicyExtensions"
name="exporterBinding" />
</bindingElementExtensions>
</extensions>
</system.serviceModel>
The following example shows the custom assertion in the WSDL file.
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
namespaces removed here for clarity...
>
<wsp:Policy wsu:Id="CustomBinding_IStatefulService_policy">
<wsp:ExactlyOne>
<wsp:All>
<acme
b:MyCustomAttribute="ExampleValue"
xmlns="http://Microsoft/WCF/Documentation/CustomPolicyAssertions" xmlns:b="http://Microsoft/WCF/Documentation/CustomPolicyAssertions">
My custom text.
<MyCustomSubElement>Custom Subelement Text.</MyCustomSubElement>
</acme>
<wsrm:RMAssertion xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm/policy">
<wsrm:InactivityTimeout Milliseconds="600000" />
<wsrm:AcknowledgementInterval Milliseconds="200" />
</wsrm:RMAssertion>
<wsaw:UsingAddressing />
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:import namespace="http://microsoft.wcf.documentation" location="" />
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.