0

What could possibly be wrong with the following method? The whole app hangs after calling the "await" line...

protected async Task<T> PostRequest<T>(string resourceEndpoint, JObject content)
    {
        Uri resourceAddress;
        if (!Helpers.TryGetUri(serviceUri + resourceEndpoint, out resourceAddress))
        {
            return default(T);
        }
        var stringContent = new HttpStringContent(content.ToString());
        HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, stringContent);
        JObject m = JsonConvert.DeserializeObject<JObject>(response.Content.ToString());
        return JsonConvert.DeserializeObject<T>(m["array"].ToString());
    }

I invoked request by SoapUI using this parameters and it went well.

This method is invoked here:

public async Task<User> Register(string name, string email, string password)
    {
        string path = "";
        JObject content = JObject.FromObject(new RegisterParams() { name = name, email = email, password = password.ToMD5Hash() });
        return await PostRequest<User>(path, content);
    }

and that here:

public void Register()
    {
        user = userService.Register(LoginTextBox, EmailTextBox, PasswordTextBox).Result;
    }
Emilia Tyl
  • 565
  • 1
  • 5
  • 17
  • Sounds like a duplicate of [http://stackoverflow.com/questions/20685059/...](http://stackoverflow.com/questions/20685059/windows-phone-httpclient-postasync-hang-with-no-response). But follow the link in RK Reddy's answer to see if it helps. – John Odom Apr 28 '15 at 22:35
  • That answer is related to GetStream, not post, I guess.. – Emilia Tyl Apr 28 '15 at 22:36
  • I know but it looks like the questioner in that link was having the same issue that you are having with PostAsync hanging on a windows phone app. So you could try setting the ConfigureAwait on PostAsync. – John Odom Apr 28 '15 at 22:38
  • 1
    Use response.Content.ReadAsStringAsync(). I doubt ToString() is the correct approach. – Jeroen Vannevel Apr 28 '15 at 22:40
  • 2
    Explanation - http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock (`.Wait` and `.Result` are equivalent from deadlock point of view). – Alexei Levenkov Apr 28 '15 at 22:40
  • @EmiliaSzymańska What version .NET are you using? `ConfigureAwait` is a .NET 4.5 feature. – John Odom Apr 28 '15 at 22:42
  • As @JeroenVannevel pointed out [HttpContent.ToString](https://msdn.microsoft.com/en-us/library/system.net.http.httpcontent%28v=vs.118%29.aspx) will not return what you are looking for, but it does not cause hang either. – Alexei Levenkov Apr 28 '15 at 22:42
  • @JohnOdom 4.5 as code uses `await`. – Alexei Levenkov Apr 28 '15 at 22:43
  • @AlexeiLevenkov Okay, I was just making sure what .NET Emilia is using since a tag for the .NET version was not added to the question. I been working with mostly 4.0 so I didn't know. – John Odom Apr 28 '15 at 22:44
  • 4
    Read, ingest, act: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html – spender Apr 28 '15 at 22:50
  • @Alexei Levenkov You're right, it's becauce of ".Result" althoug I still don't really understand why did it hang. If it was waiting for a response it should have come anyway, right? So shouldn't it unblock after it received the response? EDIT: Ok, spender 's article explains that. Thanks a lot! – Emilia Tyl Apr 28 '15 at 22:53

0 Answers0