I am running crazy with using the HttpClient in C#...
I simplified my project so my problem can be replicated easier. All i want to do is calling HttpClient.PostAsync in the Background without blocking my UI Window (I am using WPF btw).
Here is my code (slimed the code to the min.): Bing is only used here to not show my private webservice it can be replaced with every other website of course.
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
try {
MyTextBlock.Text = "Waiting...";
Uri webUri = new Uri("https://www.bing.com/");
using (HttpClient client = new HttpClient()) {
using (HttpResponseMessage response = await client.PostAsync(webUri, new MultipartFormDataContent())) {
MyTextBlock.Text = await response.Content.ReadAsStringAsync();
}
}
} catch (Exception exc) {
MessageBox.Show(exc.ToString(), "Unhandled Exception");
}
}
While my UI is waiting for the async post request it shows "Waiting" in a TextBox. And when the Async Request returns it shows the result. Nothing more should happen.
So here the Problem occurs, sometimes the PostAsync Method simply doesn't return... Even the Timeout is ignored. When I am debugging it always works but when I try the start the application it somettimes hangs. Not always which is not making find the error easier. I tried many ways with calling the request async but every time the same issue.
I also read following blog with the blocking issue in async methods but even with ConfigureAwait no differnce. http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
I just can imagine that there is a problem within the HttpClient async method locking the main thread, so it cause this problem. Wenn i use the same code in a ConsoleApplication everything is fine. There is a proxy between my client and the destination but that shouldn't be a problem at all.
Can someone replicate this problem? I am using C#/WPF with .NET Framework 4.6.1.
First at all thanks for your help, the problem seems to be solved now.
I had to do 3 things to get this work:
Get a instance of HttpClient of use it the whole application life time, so no using anymore for HttpClient.
Don't call PostAsync in the Window_Loaded Event, it seems to be to early sometimes. (I still don't get why...)
Don't use ConfigureAwait(false)
The code now looks something like this:
HttpClient client = new HttpClient();
private async void MyButton_Click(object sender, RoutedEventArgs e)
{
try {
MyTextBlock.Text = "Waiting...";
Uri webUri = new Uri("https://www.bing.com/");
using (HttpResponseMessage response = await client.PostAsync(webUri, new ipartFormDataContent())) {
MyTextBlock.Text = await response.Content.ReadAsStringAsync();
}
} catch (Exception exc) {
MessageBox.Show(exc.ToString(), "Unhandled Exception");
}
}
And to get this at start up done i had to make a really bad piece of good. But it works finally:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer startupTimer = new DispatcherTimer();
startupTimer.Tick += new EventHandler((o, a) => {
MyFunction();
startupTimer.Stop();
});
startupTimer.Interval = TimeSpan.FromSeconds(1);
startupTimer.Start();
}
When someone can replicate these behavior or can explain why these was happening, please comment it here :)
Update
Issue still occurs but it seems to be only there when the client is using some kind of proxy!