I’ve been working with WCF services a lot lately and I always specify namespaces for my service- and data-contracts. But I noticed that – by default – the WSDL of such a WCF service is generated in two parts:
- a part that resides in the namespace "
- a part that resides in the namespace I specified for the service.
The first WSDL contains an import of the second one. This also results in two separate .WSDL files when you download the meta-data (I leave any generated XML Schemas containing the data-contracts out of it in this post).
Although this works fine, I wondered whether or not it is possible to combine these WSDL files in to one file that uses the namespace I specified for my service, and get rid of this ugly "tempuri" namespace. And guess what, this is easy!
Here’s an example of a service I will use to demonstrate how to do this:
This must look familiar. Now, to make sure the WSDL that is generated for the service (once you created a host) consists of only one part that resides in the specified namespace, you must take the following steps:
- Add a
ServiceBehaviorattribute to the implementation-class, and specify the service’s namespace.
- Add the
bindingNamespaceattribute to all (non meta-data exchange) endpoints you configured for the service.
Example of step 1:
Example of step 2:
Finally, here’s a fragment of the WSDL that is generated for the service:
<xml version="1.0" encoding="utf-8" ?>
<wsdl:definitions name="MyWcfServiceImpl" targetNamespace="urn:wcfsamples:mywcfservice" >="http://schemas.xmlsoap.org/wsdl/"
>="http://schemas.xmlsoap.org/wsdl/soap/" >="http:// …
That’s it. When you now download the meta-data for this service, you get only one .WSDL file. Exactly what I wanted.
I hope you find this information useful. And remember: let’s keep our WSDL tidy!
But why isn’t this included in any MS-samples or courses? I think everyone would want this…
Great example, I found a little more about how WSDL namespaces map to the various parts of the WCF service. I hope this helps to truely understand why microsoft made the namespace configurable in so many places