Inconsistent results of HttpClient GetAsync() with IIS Asp.net host and Console host

Topics: Web Api
Dec 28, 2011 at 4:02 PM

I have followed the Hands-On Lab:  Build RESTful APIs with WCF Web API v1.1, published on 12/6/2011:

With this lab, it shows how to host the service in a console app and a test client (another console app) connect to the service with HttpClient's GetAsync to download contact data.  But I am geting different results with same test client.  Service hosted in console application (provided by this lab) working fine, but service hosted at asp.net doesn't work:   I migrated the service to an Asp.Net web app hosting in IIS7 (window 7), everything else unchanged, except I added one line:

            RouteTable.Routes.Add(new ServiceRoute("api/contacts", new WebServiceHostFactory(), typeof(ContactService)));
into web app's global.asax.

However, the client threw an exception this time at this line:

                        r.Result.Content.ReadAsAsync<Contact>().ContinueWith(l => ...

Client Error:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at ContactClient.Program.<ListOneContacts>b__5(Task`1 l) in C:\ContactClient\Program.cs:line 65
   at System.Threading.Tasks.ContinuationTaskFromResultTask`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()

Quickly review the http traffic with fiddler, I found out one major difference within the response messages:

Console hosting response message (content) is a well formated xml text with "<?xml version="1.0" encoding="utf-8"?>", but asp.net's response missing this part, which so caused HttpClient's XmlSerializer can't deserialize the xml string properly.

I am not sure if there are some settings on asp.net site I can apply to control this behavior or just a bug? 

There are the http debug info from fiddler

[Host with Asp.NET IIS7]

GET http://localhost/ContactWeb/api/contacts/1 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost
Cookie: .ASPXANONYMOUS=UOobRDzlzAEkAAAANjZiYmE5YjYtNjBjZi00MGZjLWIyZDMtMGYyZDg1NzE1YzA1Pj9od19reEcDIuQlk9jGI8A1IuDBFVkerkz0pGTvo8c1; .ASPXAUTH=A6DC38458CD7D54758B6019628316B3F86921E8617B11FF4EF586BE085587C4E42D627D927100057B54CDB2EACBF3A8356D818E51FA3E8BDF9762C6ECF6A1070D215021FA4B1D2F48436358A8E0D1F01C60DC4FE149D7AACB6E2B0800110DFA4EADB741A952D0E03B7C81CE6040FBDE3795F25AE491025D9A09E47E78720302199D17A11B9D100CA6A43C5B18B8D46E746E87580BA50BD42016AB07424649856; ASP.NET_SessionId=40xgp4tncxkc02xf2qsm5hvr

 

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 378
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 28 Dec 2011 15:13:38 GMT

<Contact xmlns="http://schemas.datacontract.org/2004/07/ContactManager" xmlns:i="1900-01-01T00:00:0011111111-1111-1111-1111-1111111111111Alicehttp://www.w3.org/2001/XMLSchema-instance"><CreatedOn>1900-01-01T00:00:00</CreatedOn><ExternalGuid>11111111-1111-1111-1111-111111111111</ExternalGuid><Id>1</Id><Name>Alice</Name><Notes xmlns:a="hellohttp://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:string>hello</a:string></Notes></Contact>

[Host with Console Application]

GET http://localhost:8080/api/contacts/1 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost:8080
Cookie: .ASPXANONYMOUS=UOobRDzlzAEkAAAANjZiYmE5YjYtNjBjZi00MGZjLWIyZDMtMGYyZDg1NzE1YzA1Pj9od19reEcDIuQlk9jGI8A1IuDBFVkerkz0pGTvo8c1; .ASPXAUTH=A6DC38458CD7D54758B6019628316B3F86921E8617B11FF4EF586BE085587C4E42D627D927100057B54CDB2EACBF3A8356D818E51FA3E8BDF9762C6ECF6A1070D215021FA4B1D2F48436358A8E0D1F01C60DC4FE149D7AACB6E2B0800110DFA4EADB741A952D0E03B7C81CE6040FBDE3795F25AE491025D9A09E47E78720302199D17A11B9D100CA6A43C5B18B8D46E746E87580BA50BD42016AB07424649856; ASP.NET_SessionId=2qrnfm1iwhxo3mxeuuzy0ogf


HTTP/1.1 200 OK
Content-Length: 328
Content-Type: application/xml; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 28 Dec 2011 15:20:19 GMT

<?xml version="1.0" encoding="utf-8"?><Contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="1Alice1900-01-01T00:00:0011111111-1111-1111-1111-111111111111hellohttp://www.w3.org/2001/XMLSchema"><Id>1</Id><Name>Alice</Name><CreatedOn>1900-01-01T00:00:00</CreatedOn><ExternalGuid>11111111-1111-1111-1111-111111111111</ExternalGuid><Notes><string>hello</string></Notes></Contact>

Any ideas?

Chang