Windows phone HttpClient PostAsync hang with no response

可紊 提交于 2019-12-07 10:06:02

问题


I am having problem in calling the HttpClient post method from WP application.The PostAsync always hangs and does not give any response.The same code works when i try it from WPF application. Here is what I am doing:

Server Web API 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 WP8 code calling 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;
        }

    }

回答1:


Checkout This Answer res.olved my issue. Use ConfigureAwait

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


来源:https://stackoverflow.com/questions/20685059/windows-phone-httpclient-postasync-hang-with-no-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!