Over a million developers have joined DZone.

How To Fix the WCF Cache of Dynamic WSDLs

DZone 's Guide to

How To Fix the WCF Cache of Dynamic WSDLs

· ·
Free Resource

One of the least used WCF extension points is IWsdlExportExtension. This extension allows to customize the WSDL document which WCF emits. Since you rarely want to do that, this extension is not commonly used. When it is already used it is usually in the context of flattening the WSDL. A different use case I have recently seen is to push dynamic content into the WSDL. More specifically a user was trying to generate XSD schemas from a live database table and to put it to the WSDL so clients would always get the latest schema. The WCF service itself was treating the request as XML anyway so it did not care for such changes. The requirement was for the wsdl to reflect the latest DB changes at any time. Our problem was that once the WSDL was generated for the first time it would not be regenerated. This resulted in a stale schema.

This is how we created the WSDL exporter:

static void Main(string[] args)
  using (ServiceHost host = new ServiceHost(typeof(Service)))
    host.Description.Behaviors.Add(new ServiceMetadataBehavior() {
                        HttpGetEnabled = true,
                        HttpGetUrl = new System.Uri("http://localhost:7171/")});
    var ep = host.AddServiceEndpoint("IService", new BasicHttpBinding(),
    ep.Behaviors.Add(new MyWsdlExporter());
    Console.WriteLine("\nPress enter to close service...\n");

class MyWsdlExporter : IWsdlExportExtension, IEndpointBehavior
   public void ExportEndpoint(WsdlExporter exporter,
                              WsdlEndpointConversionContext context)
       context.WsdlPort.Documentation = "crearted on " + DateTime.Now.ToString();
When we run this service and open the WSDL we get this:
<wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService">
<wsdl:documentation>crearted on 05/05/2012 21:08:13</wsdl:documentation>
When we refresh the WSDL after a few seconds we still get this:
<wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService">
<wsdl:documentation>crearted on 05/05/2012 21:08:13</wsdl:documentation>
This is not a browser or proxy cache. WCF does not recreate the WSDL - which can also be seen by putting a breakpoint (which is only called once) on the exporter.

This behavior makes sense when you consider the case where there is no importer extension - then the wsdl is generated based on the data contract assembly, and as long as that assembly does not change the wsdl will not also. However we have chose to put dynamic logic in ExportEndpoint method so that default behavior did not work well for us.

One way to fix that is to use a message inspector to update the wsdl before it is sent to the client. In this case IWsdlExportExtension is not required at all. This approach is described here.

An alternative could be to build a WCF REST endpoint in the same service to act as a proxy to the real WSDL.

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}