blog community
WCF: namespaces in WSDL

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:

[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:

  1. Add a ServiceBehavior attribute to the implementation-class, and specify the service's namespace.
  2. 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"/>

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" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:soap
="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="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!


Posted 20-07-2008 23:50 by edwinw

Comments

manuelr wrote re: WCF: namespaces in WSDL
on 21-07-2008 10:03

Nice :)

Robert te Kaat wrote re: WCF: namespaces in WSDL
on 31-07-2008 13:13

C00l.

But why isn't this included in any MS-samples or courses? I think everyone would want this...

willemm wrote re: WCF: namespaces in WSDL
on 03-08-2008 12:53

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

Alex wrote re: WCF: namespaces in WSDL
on 14-04-2009 16:14

Thanks a lot man. Works perfectly. :)

straa wrote re: WCF: namespaces in WSDL
on 21-07-2009 13:53

Good, thanks

Robert te Kaat wrote re: WCF: namespaces in WSDL
on 20-10-2009 13:17

I following your directions and also read the article mentioned by willemm, but I still get tempuri namespaces. Not in the WSDL, but in the actual message. It's probably has something to do with the usage of MTOM. The generated client works fine, btw.

I specified "urn:fleXdoc" as the namespace, however this is the actual message that is received from the service (captured with Fiddler):

HTTP/1.1 200 OK

Cache-Control: private

Transfer-Encoding: chunked

Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="uuid:bdeedaed-13b0-43b3-9f3e-d837e6133be6+id=2";start-info="text/xml"">tempuri.org/.../xml"

MIME-Version: 1.0

205

--uuid:bdeedaed-13b0-43b3-9f3e-d837e6133be6+id=2

Content-ID: <http://tempuri.org/0>

Content-Transfer-Encoding: 8bit

Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="schemas.xmlsoap.org/.../"><s:Body><BuildDocumentResponse xmlns="urn:fleXdoc"><BuildDocumentResult><xop:Include href="cid:http%3A%2F%2Ftempuri.org%2F1%2F633916400755326138" xmlns:xop="www.w3.org/.../s:Envelope>

8000

--uuid:bdeedaed-13b0-43b3-9f3e-d837e6133be6+id=2

Content-ID: <tempuri.org/.../633916400755326138>

Content-Transfer-Encoding: binary

Content-Type: application/octet-stream

PK����fT;Y�������[Content_Types].xml �  *** cut off here ***

E Schlosser wrote re: WCF: namespaces in WSDL
on 09-11-2009 23:52

Robert,

Did you find an answer to this?

Eric

edwinw wrote re: WCF: namespaces in WSDL
on 10-11-2009 8:07

Eric,

I was discussing this issue offline with Robert. It seems like the MTOM encoder uses the tempuri namespace for generating unique Content-ID's for the attachments in the MTOM message.

I looked into this with .Net Reflector and found the following constant declaration on the internal class MtomMessageEncoder:

private const string mtomStartUri = "http://tempuri.org/0";

I also found several methods on this class that use a hardcoded tempuri namespace, for example:

internal void WriteMessage(Message message, Stream stream, string boundary)
{
   this.WriteMessage(message, stream, this.GenerateStartInfoString(), boundary, "http://tempuri.org/0", false);
}

So I'm affraid there's no standard way in WCF to configure this namespace.

Greetings,

Edwin

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Enter code (required)
Powered by Community Server (Commercial Edition), by Telligent Systems