WCF WebHttp Service returns HTTP 415 “Unsupported Media Type”

When I was developing my demos for PDC10 I ran into a problem.  I was using some new HTTP activities for the next release of WF and invoking a simple console application with an HTTP POST.  For some strange reason I ran into the following error

Request

 POST https://127.0.0.1:8080/Marketing HTTP/1.1
Content-Type: application/xml
Host: 127.0.0.1:8080
Content-Length: 91
Expect: 100-continue
Connection: Keep-Alive

<string xmlns="https://schemas.microsoft.com/2003/10/Serialization/
">Phyllis Harris</string>

Response

 HTTP/1.1 415 Cannot process the message because the content type 
'application/xml' was not the expected type 'text/xml; charset=utf-8'.
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 27 Oct 2010 20:00:06 GMT

I have done a fair bit of development with WCF but I had never seen this error message before.  I was so sure that my code was correct I immediately assumed it must be a problem with our new HttpPost activity so first I decided to check with our test team to see if they had a working test.  When I found out they did we began looking more closely and I found… a copy paste bug.

That’s right, in my hosting code it turned out that I was using ServiceHost instead of WebServiceHost to host my WCF WebHttp service.  In most cases when service host detects that your service (or it’s configuration) is somehow invalid it throws an exception when you try to open the host.  For some strange reason (bug?) in this case it does not throw an exception but happily starts accepting requests and throwing 415 when you invoke it.

The solution of course is to use WebServiceHost instead of ServiceHost.  Hopefully this will save somebody out there some time.