I have gone through several SO and forums - spending over several hours attempting the several answers/comments I found on these sites. I still haven't been able to resolve the issue.
Note 1: It worked on my laptop before - all further developments were made on a separate branch. Neither the original branch nor the new branch works. It works on my team mate's laptop and we still haven't been able to figure out the issue.
Note 2: I have tried re-cloning the original branch in a separate directory and that fails too. Again, we couldnt reproduce the issue on my team mate's system.
Environment - Windows 10, Visual Studio 2019, WPF App (.NET Framework 4.8), C# 8
public class SomeClient
{
private readonly string _host;
private readonly HttpClient _client;
public SomeClient(HttpClient client, string host)
{
_host = host;
_client = client;
}
public async Task<string> GetResponse(string request)
{
HttpContent content = new StringContent(request, Encoding.UTF8, "application/xml");
HttpResponseMessage body = await _client.PostAsync(_host, content);
return await body.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
Calling Code (Test Method)
[TestClass]
public class SomeClientShould
{
[TestMethod]
public async Task ReturnResponse()
{
string request = "";
SomeClient client = new SomeClient(new HttpClient(), @"http:\\localhost:9888\");
string response = await client.GetResponse(request);
Assert.AreEqual("<RESPONSE>Server is Running</RESPONSE>", response);
}
}
Here I am not making any request, sending an empty string which should respond with the string shown in the Assert (passes on my team mate's system, Postman on my system works also).
I have tried various combo's of ConfigureAwait(false), also there is no blocking code - its async all the way. Even a simple test method shown above exits after the PostAsync line without executing the next line. Try-Catch is not catching any exception and Im having a breakdown.
So dummy me, first of all made a mistake with the host uri - the slashes had to be the other way round in the test project - "http://localhost:9888/". So the tests run now.
Next issue, between my projects I had a version mismatch of the Microsoft.Bcl.AsyncInterfaces library.
Somehow one project had the latest version referenced but the other didn't? Perhaps the issue came about during merging of the original branch - before my teammate and I created our respective dev branches for further development. Anyway it works now.