C
C#2y ago
Daryl

❔ httpClient.PostAsync() - Program exits and never hits my breakpoint after?

This is the code:
public async Task<string> RegisterAgent(string symbol, string faction)
{
string url = apiUrl + "register";
AgentRequest newAgent = new AgentRequest(symbol, faction);

var content = JsonContent.Create(newAgent, mediaTypeHeaderValue);
var response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();

System.Console.WriteLine(response.Content);
return "";
}
public async Task<string> RegisterAgent(string symbol, string faction)
{
string url = apiUrl + "register";
AgentRequest newAgent = new AgentRequest(symbol, faction);

var content = JsonContent.Create(newAgent, mediaTypeHeaderValue);
var response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();

System.Console.WriteLine(response.Content);
return "";
}
When I step through the code with my debugger it gets as far as var response... and then the application exits.... It should get to the code below. I don't know if this changes things but core is a class in a Class Library. I am using a Unit Test to run the test. Sorry if this is a really stupid question but I'm stumped.
6 Replies
Daryl
DarylOP2y ago
I have just realised that the Unit Test file was still called Core.cs - I changed the Unit Test class to CoreUT to avoid any naming confusion. I've now renamed the .cs file and rebuilt the application but it's still exiting on the PostAsync()....
Daryl
DarylOP2y ago
Ok not sure exactly what I did but it's working now... I changed the return in the function from "" to the string symbol andalso changed the unit test to use Assert.Equal(). I can see it now gets past PostAsync() and is now throwing an error due to response.EnsureSuccessStatusCode(); not returning 200. I have to go to bed now but will leave this up as I'm not sure what I previously did wrong. Any advise would be great and I'll pick it up in the morning, thanks!
phaseshift
phaseshift2y ago
Yeah, it's because you're calling register-agent in your test without awaiting it
Daryl
DarylOP2y ago
Thank you but I haven't changed that and now it's working? 🤔
Omnissiah
Omnissiah2y ago
exactly, in this case put a breakpoint on the line after the await
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server