C
C#14mo 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
Daryl14mo 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
Daryl14mo 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
phaseshift14mo ago
Yeah, it's because you're calling register-agent in your test without awaiting it
Daryl
Daryl14mo ago
Thank you but I haven't changed that and now it's working? 🤔
ffmpeg -i me -f null -
exactly, in this case put a breakpoint on the line after the await
Accord
Accord14mo 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.