HttpClient PostAsync Windows Phone Hangs Unanswered

I had a problem calling the post method HttpClientfrom a WP application. PostAsyncalways freezes and does not give an answer. The same code works when I try to use it from a WPF application. That's what I'm doing:

Server Web Server Code

public class GameController : ApiController
{
[HttpPost]

public GameDto CreateGame(GameDto gameDto)
        {
            try
        {
            GameManager bl = new GameManager();
            gameDto = bl.CreateGame(gameDto);
            return gameDto;
        }
        catch (Exception)
        {

            throw;
        }
    }
}

Client call of WP8 code from class library

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {


 HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:59580");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));               
GameDto newGame = new GameDto();
                newGame.CreatedBy = 1;
            newGame.Name = txtGameName.Text;
            newGame.GameTypeId = (int)cmbGameType.SelectedValue;
             MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
             var response = await client.PostAsync<GameDto>("api/Game/CreateGame", newGame, jsonFormatter);
            response.EnsureSuccessStatusCode(); // Throw on error code.
            var userDto = await response.Content.ReadAsAsync<GameDto>();
            //_products.CopyFrom(products);
            MessageBox.Show(userDto.Id.ToString());
        }
        catch (Exception)
        {

            throw;
        }

    }
+4
source share
1 answer

Checkout Answer res.olved my problem. Use ConfigureAwait

var result = await httpClient.GetStreamAsync("weeklyplan")
                         .ConfigureAwait(continueOnCapturedContext:false);
+4
source

All Articles