public async static Task<string> GetResponse(string host, string request, HttpClient client)
{
HttpContent content = new StringContent(request, Encoding.UTF8, "application/xml");
HttpResponseMessage message = await client.PostAsync(host, content).ConfigureAwait(false); // issue here
string response = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
return response;
}
The calling code within the same namespace -
public async Task<string> Post(Transaction transaction)
{
string postXml = PostXml.Create(transaction); // serializes object to XML
string response = await GetResponse(host, postXml, client); // client initialized in constructor of this class
return await Deserialize.Response(response);
}
The above methods run as expected when calling them directly from Unit Tests.
I have an interface to take advantage of dynamic loading of different services - which calls the Task<string> Post(Transaction transaction)
method. When calling this method from my main project - the relevant service is loaded (depending on settings), but I get no response from client.PostAsync(host, content)
.
No exception or errors. I have tried to debug the issue with breakpoints but got nothing. It hits the PostAsync
and continuing just escapes the rest of the code.
Note 1: everything is async all the way through and there is no thread blocking code such as .Wait()
or .Result
anywhere
Note 2: i have tried running it by removing the ConfigureAwait as well. In both cases, the unit tests run perfectly. Somehow it doesnt work when i reference this project in my main project. I still dont understand, cause I still reach that part of the code. Beyond which, the program still runs without blocking the UI thread but there is no response.
UPDATE
To get the host
, I was reading a configuration json file. Reading this file caused the issue I am facing, when I hardcode the host to a variable, everything works fine. Initially the reading of this file was Asynchronous, but to debug the issue I changed it to synchronized reading - this still did not resolve the issue.
NOTE - Using the App.Config works well.
I am using newtonsoft to read a custom json settings file. I am not a big fan of reading configuration from the app.config due to its XML structure and complex way of accessing custom section.
public static ServerConfig GetServerSettings()
{
string json = File.ReadAllText("appSettings.json");
return JsonConvert.DeserializeObject<ServerConfig>(json);
}
ServerConfig has properties namely the string host
and string company
(i.e. which company's data) I want to access.