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 "
http://tempuri.org/
", - 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
ServiceBehavior
attribute to the implementation-class, and specify the service’s namespace. - Add the
bindingNamespace
attribute to all (non meta-data exchange) endpoints you configured for the service.
Example of step 1:
|
Example of step 2:
<endpoint 
   address=""      binding="wsHttpBinding"     contract="MyWcfService.IMyWcfService"     bindingNamespace="urn:wcfsamples:mywcfservice"/> |
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!
3 comments
Nice 🙂
manuelr
C00l.
But why isn’t this included in any MS-samples or courses? I think everyone would want this…
robertka
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
http://www.pluralsight.com/community/blogs/kirillg/archive/2006/06/18/28380.aspx
willemm