Windows phone的HttpClient的PostAsync没有响应挂 [英] Windows phone HttpClient PostAsync hang with no response
本文介绍了Windows phone的HttpClient的PostAsync没有响应挂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有呼吁从WP application.The PostAsync的
总是挂起,不给出任何response.The同一code ++工程,当我从WPF应用程序尝试。下面是我在做什么:的HttpClient
post方法的问题
服务器Web API code
公共类GameController:ApiController
{
[HttpPost]公共GameDto CreateGame(GameDto gameDto)
{
尝试
{
游戏管理BL =新游戏管理();
gameDto = bl.CreateGame(gameDto);
返回gameDto;
}
赶上(例外)
{ 扔;
}
}
}
客户端WP8 code从类库调用
专用异步无效Button_Click(对象发件人,RoutedEventArgs E)
{
尝试
{
HttpClient的客户端=新的HttpClient();
client.BaseAddress =新的URI(HTTP://本地主机:59580);
client.DefaultRequestHeaders.Accept.Add(
新MediaTypeWithQualityHeaderValue(应用/ JSON));
GameDto newGame =新GameDto();
newGame.CreatedBy = 1;
newGame.Name = txtGameName.Text;
newGame.GameTypeId =(INT)cmbGameType.SelectedValue;
MediaTypeFormatter jsonFormatter =新JsonMediaTypeFormatter();
VAR响应=等待client.PostAsync< GameDto>(API /游戏/ CreateGame,newGame,jsonFormatter);
response.EnsureSuccessStatus code(); //扔在错误code。
VAR userDto =等待response.Content.ReadAsAsync< GameDto>();
//_products.CopyFrom(products);
MessageBox.Show(userDto.Id.ToString());
}
赶上(例外)
{ 扔;
} }
解决方案
此结账
<一href=\"http://stackoverflow.com/questions/9895048/async-call-with-await-in-httpclient-never-returns?rq=1\">Answer res.olved我的问题。
使用ConfigureAwait
VAR的结果=等待httpClient.GetStreamAsync(weeklyplan)
.ConfigureAwait(continueOnCapturedContext:假);
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;
}
}
解决方案
Checkout This Answer res.olved my issue. Use ConfigureAwait
var result = await httpClient.GetStreamAsync("weeklyplan")
.ConfigureAwait(continueOnCapturedContext:false);
这篇关于Windows phone的HttpClient的PostAsync没有响应挂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文