.NET Zone is brought to you in partnership with:

Yaron is a Microsoft MVP in the Connected System Developer realm. Yaron authors the successful Web Services 2.0 blog, in which he helps the community with some of his favorite topics: Web services, security and interoperability. Yaron is also an architect in HP. Yaron is a DZone MVB and is not an employee of DZone and has posted 27 posts at DZone. You can read more from them at their website. View Full User Profile

How To Fix the WCF Cache of Dynamic WSDLs

05.05.2012
| 2427 views |
  • submit to reddit

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(),
                        "http://localhost:7171/");
    ep.Behaviors.Add(new MyWsdlExporter());
    host.Open();
                                        
    Console.WriteLine("\nPress enter to close service...\n");
    Console.ReadLine();
  }
}

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 Yaron Naveh, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)