I am working on a Nancy / ASP.Net project. I tried using WebRequest to retrieve data from another web service, and I executed the request in an async function. I find the problem that when I try to wait for a task returned from a function, it blocks indefinitely. Simplified code looks like this.
using System.Net;
using System.Threading.Tasks;
using Nancy;
public class TestModule : NancyModule{
public TestModule() {
Get["/"] = p => "Hello, world";
Get["/async/"] = p => {
var req = WebRequest.CreateHttp("http://localhost:13254/");
var responseTask = getResponse(req);
var waitSuccess = responseTask.Wait(10000);
return waitSuccess ? "Yeah!" : "woooh!";
};
}
async Task<HttpWebResponse> getResponse(HttpWebRequest request) {
return (HttpWebResponse) await request.GetResponseAsync();
}
}
The code uses NancyFx, but it also happens on the ASP.Net page on the page. The service on localhost: 13254 is working fine. If I use the task directly returned from the GetResponseAsync () request, the code works fine, but if I wrap it in an asynchronous method, it just blocks.
- , ?:( , async ... , ..