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 ‘tempuri’ namespace. And guess what, this is easy!
Here’s an example of a service I will use to demonstrate how to do this:
[ServiceContract(Namespace="urn:wcfsamples:mywcfservice")] public interface IMyWcfService { [OperationContract] string GetData(int value); } // implementation of the service public class MyWcfServiceImpl : IMyWcfService { public string GetData(int value) { return string.Format("You entered: {0}", value); } }
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:
[ServiceBehavior(Namespace="urn:wcfsamples:mywcfservice")]
Example of step 2:
<endpoint address="" binding="wsHttpBinding" contract="MyWcfService.IMyWcfService" bindingNamespace="urn:wcfsamples:mywcfservice"/>
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!