HttpClient only working in Main but not in another Class? [Answered]
I'm currently following a c# json tutorial and trying to put this code into it's own class so I can keep multiple code references in a single project. Everything works fine in Main but when I try to put it into another class it won't work properly, anyone have an idea on what I might be missing?
When I run it in main I get the json details printed in the console window, but when i run it in the class it's blank.
The only thing in the main method is JsonServer.Run();
18 Replies
that's because your program ended execution before the Run Method did because its running asynchronously, you need to await the method
ahh that makes sense, not sure how to await a method though. 🙂
(first time using await lol)
await JsonServer.Run();
ofc the Main Method also needs to be an async Task
is there any way around that so i can keep the main method mostly untouched?
JsonServer.Run().Result;
but i wouldn't recommend it
may cause deadlockshmm
why do you want ur main method to be untouched?
Gonna have like 10 different unrelated little things in the project that i can access from a menu do tests and check everything in a single project.
reading input, reading jsons, various db things and actions
all for testing/learning and as a reference for later projects
so i dont have a dozen separate projects
i don't think an async Task Main method would affect any of them
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
something like this?
u forgot to await
.
working now
looks perfect
🙂
ty for the help,
u can write /close to close this thread
✅ This post has been marked as answered!