I'm working on a Nancy/ASP.Net project. I've tried to use WebRequest to get data from other web service and I've implemented the request in an async function. I find a problem that when I try to wait on a task returned from the function, it gets blocked indefinitely. The 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 = req.GetResponseAsync(); // this works!
var responseTask = getResponse(req); // this gets blocked!
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 happens as well on vanilla ASP.Net page. The service on localhost:13254 is working fine. If I use the task directly returned from request's GetResponseAsync(), the code works fine, but If I wrap it in an async method, it just gets blocked.
Does anyone have any idea what's wrong with the code? :( I can change to use synchronous version but the async function works find in other self-hosting services... so I'd like to use the same code here too if possible..
I describe this deadlock behavior on my blog and in a recent MSDN article.
To fix this, you can either use ConfigureAwait(false) everywhere, or you can use synchronous methods. The ideal solution is to use await all the way and never use Wait or Result, but that may not be possible in your situation (it would only work if Nancy worked with async delegates, i.e., Get["/async/"] = async p => { ... };).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With