【发布时间】:2017-09-20 12:54:39
【问题描述】:
我想做几个 POST-Request。
我的问题是应用程序在我的async void bt_publishPath_Click()-Event 中的await 期间在Task<...> do_POST()-Function 中随机崩溃。
我可以做一些 POST 请求(一切正常),有时await 会阻塞,应用程序会因System.NullReferenceException 而崩溃(经过长时间的等待)。我不明白为什么会这样,因为它第一次工作。
这是我目前得到的结果:
我已经注释掉了所有不重要的功能。我已经在没有这些代码行的情况下测试了我的代码,并且也发生了错误。
// MainWindow
private async void bt_publishPath_Click(object sender, RoutedEventArgs e)
{
// gr_spinner.Visibility = Visibility.Visible;
ApiResponse res = await ApiCall.do_POST(
"/path",
getBasicAuthString(),
flink.getJsonString()
);
// gr_spinner.Visibility = Visibility.Collapsed;
// Globals.errorHandling(res.Statuscode, res.Parameters[0]);
}
// ApiCall static Class
public static Task<ApiResponse> do_POST(string apiPath, string auth, string json)
{
return Task.Run(() =>
{
int statuscode = 0;
HttpWebResponse response = null;
try
{
// HEADER
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiBase + apiPath);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", auth);
// DATA
byte[] data = Encoding.ASCII.GetBytes(json);
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
// SEND AND GET RESPONSE
/*
* It seems to be that GetResponse() isn't called correctly.
* The Backend doesn't receive a POST-request and the reponse stays null.
*/
response = (HttpWebResponse)request.GetResponse();
statuscode = (int)response.StatusCode;
}
catch (WebException ex)
{
statuscode = (int)((HttpWebResponse)ex.Response).StatusCode;
}
return new ApiResponse(statuscode, null, new String[] { apiPath });
});
}
其他信息:
在使用 async 和 await 执行此操作之前,我尝试使用 BackgroundWorker 解决此问题。在这种情况下,我遇到了类似的问题:有时上一个 POST 的 backgroundWorker 仍然很忙,程序无法启动一个新的(死锁)。
我还发现了一些有趣的东西:async await Task null reference exception
- 但在这种情况下,它是一个 ASP.NET 应用程序。我正在开发一个 Windows 桌面客户端 - 而不是服务器应用程序。所以这并没有真正的帮助。
感谢您的帮助!
【问题讨论】:
-
为什么
ApiResponse res = task.Result;当你是await呢? -
1) 你不应该在异步方法中调用
.Result。 -
2) 你不应该使用
async void,除了按钮_点击这里。你忘记发了。它是必不可少的,因为它是调用树的根。 -
按钮点击方法是异步的吗?你怎么称呼
postAsync?您的代码是同步和异步的真正不必要的混搭。 -
@Rahul 在等待之后调用。我不知道获得结果的另一种方法。
标签: c# .net wpf asynchronous .net-4.5