我正在为Mono编写一个控制台应用程序,试图让HttpClient下载一些内容并用Json.NET反序列化它。
我遇到的问题是当我调用异步方法时出现Stack Overflow。
就像这样,简化为isse的核心:
public static void Main(string[] args){
...
Manager man = new Manager(){...}; // setting up target url, timeouts etc
man.RenewAuth().Wait(); //it's async but the first time it runs synchronously
...
}
public class Manager{
...
async Task<T> GetAsync<T> (string urlAdd)
{
var httpRes = await Client.GetAsync (Client.BaseAddress + urlAdd);
string s = await httpRes.Content.ReadAsStringAsync ();
var deserialized = JsonConvert.DeserializeObject<T> (s);
return deserialized ;
}
public async Task RenewAuth () //
{
... // logging in, setting up etc
// THIS WORKS:
var aKey = GetAsync<AuthKey> ("/AuthKey?email=" + email).Result;
// THIS DOESN'T WORK, CAUSES STACK OVERFLOW:
var aKey = await GetAsync<AuthKey> ("/AuthKey?email=" + email);
... // store the api auth key from aKey and return
}
}
在控制台中我得到了这个:
Stack overflow in unmanaged: IP: 0x9a823e19, fault addr: 0xb0221ffc
Stack overflow in unmanaged: IP: 0x93b36482, fault addr: 0xb0220ff4
Stack overflow in unmanaged: IP: 0x93b36482, fault addr: 0xb021fff4
Stack overflow in unmanaged: IP: 0x93b3388a, fault addr: 0xb021effc
Stack overflow in unmanaged: IP: 0x93b36482, fault addr: 0xb021dff4
Stack overflow in unmanaged: IP: 0x93b36482, fault addr: 0xb021cff4
Stack overflow in unmanaged: IP: 0x93b3388a, fault addr: 0xb021bffc
Stack overflow in unmanaged: IP: 0x93b36482, fault addr: 0xb021aff4
Stack overflow: IP: 0x9a823e19, fault addr: 0xb0218ffc
Stacktrace:
Press any key to continue...
答案 0 :(得分:1)
事实证明这只在连接调试器时发生,否则代码工作正常。