Preview 6 - HttpContent.ReadAs<T>

Topics: Web Api
Nov 29, 2011 at 9:34 PM
Edited Nov 30, 2011 at 1:01 AM

 

Hi I'm switching to preview 6 and running into an issue i'm not too sure about how to solve.

	var resource = response.Content.ReadAs<Dao>(new[] { 
                new DaoFormatter() 
        });

I used to do this. But since async is the new shiz I've tried to port that code to what is below. The code below is always null. Please advise where I'm going south.

	[Test]
        public void rest_endpoint_call_from_httpClient_parsed_correctly()
        {
            //arrange
            var baseUri = "url";
            using (var httpClient = new HttpClient())
            {

                httpClient.BaseAddress = new Uri(baseUri);

                //act
                var response = httpClient.GetAsync("?querystring").Result;
                response.EnsureSuccessStatusCode();

                var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);
                
                var resource = json.ReadAs<Dao>(new Dao());

                //assert
                Assert.That(resource, Is.Not.Null, "resource should never be null");
                Assert.That(resource.candidates.Count, Is.GreaterThan(0), "usually returns 2 results");
                Assert.That(resource.candidates[0].address, Is.EqualTo("address"), "Should match input address");
            }
        }

 

I was able to make this work again by using Newtonsoft's Json.net. Is JsonValue not able to do this or am I using it wrong?

JsonConvert.DeserializeObjet<Dao>(response.Content.ReadAsStringAsync().Result);

 

Am I seeing something that is expected like the answer to http://stackoverflow.com/questions/5026985/how-to-serialize-using-system-json-in-monotouch

Nov 30, 2011 at 3:33 PM

Try to do this:

[Test]
public void rest_endpoint_call_from_httpClient_parsed_correctly()
{
    //arrange
    var baseUri = "url";
    var httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri(baseUri);

    //act
    httpClient.GetAsync("?querystring")
        .ContinueWith(r =>
            {
                if (r.IsCompleted && r.Result.IsSuccessStatusCode)
                {
                    r.Result.Content.ReadAsOrDefaultAsync<Dao>()
                        .ContinueWith(r2 =>
                            {
                                var resource = r2.Result;

                                //assert
                                Assert.That(resource, Is.Not.Null, "resource should never be null");
                                Assert.That(resource.candidates.Count, Is.GreaterThan(0), "usually returns 2 results");
                                Assert.That(resource.candidates[0].address, Is.EqualTo("address"), "Should match input address");
                            });
                }
            });
}

Nov 30, 2011 at 4:37 PM
Edited Nov 30, 2011 at 4:52 PM

Thanks, I needed to add a reference to System.Net.Http.Formatting to get that method. 

 

Why did you remove the using statement? Does the httpClient dispose of itself now?