❔ hosting an asp.net core api in a winform app.

How can I host an asp.net core api in a winform app, where the api starts and stops based on user gui input and where the api console is displayed on the gui?
32 Replies
JakenVeina
JakenVeina2y ago
the same way you host an ASP.NET Core API in any other app except you move the application host into your GUI logic instead of Main() and you redirect the console
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
agreed
madogumglynium
madogumglyniumOP2y ago
the way you suggest that makes it sound really easy, I would really appreciate some help at your earliest convenience.
JakenVeina
JakenVeina2y ago
if you have a question, ask it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
I mean, you definitely don't need to make separate projects
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
if i create two separate projects within one solution, would i be able to export the whole solution as one executable?
JakenVeina
JakenVeina2y ago
yes
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JakenVeina
JakenVeina2y ago
definitely
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
would you be willing to spend around 10 minutes helping me in a vc? i already have the winform app running
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
ok, ill try to follow the steps you've mentioned
JakenVeina
JakenVeina2y ago
an ASP.NET Core app is pretty trivial to bootstrap
using var application = new WebApplicationBuilder()
.ConfigureServices(...)
.Configure(...)
.Build();

using var stopTokenSource = new CancellationTokenSource();

await application.RunAsync(stopTokenSource.Token);
using var application = new WebApplicationBuilder()
.ConfigureServices(...)
.Configure(...)
.Build();

using var stopTokenSource = new CancellationTokenSource();

await application.RunAsync(stopTokenSource.Token);
madogumglynium
madogumglyniumOP2y ago
i dont understand what you mean. by trivial to bootstrap
JakenVeina
JakenVeina2y ago
split that up across your start and stop methods in your GUI and you're done I mean the code I JUST gave you
madogumglynium
madogumglyniumOP2y ago
im currently setting up the api project within my solution once im set up, then im going to try what you suggested by the way, do i need to make a desktop project reference the api project?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
i managed to create both projects in one solution, now correct me if im wrong: what i need to do is move the api logic into the winform app?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
i did exactly what you told me not to.. (because i dont know how to implement what you've suggested) what i tried: 1 solution , 2 projects : - building the api in the winform program.cs on a seperate thread.. =>(feels wonkey) - creating a new process that references the api executable.. =>(eventually i might not have access to the api exe which would ruin this whole setup.) what am going to try next: creating the api within the winform app, so that i could have access to the whole api in order to manage it. (tips would be appreciated)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
ok, what next? how can i manage the api from the winform gui? (start api, stop api, restart, log, etc....)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
am not familiar, i recently jumped on the core train, i been using .net framework for a while. and this spicific usecase it new territory for me. just finished testing that, api runs, but the gui freezes. i tried both runasync, and startasync both freeze the desktop app i got pretty close with creating a separate process.. i might expore a bit more towards that
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
madogumglynium
madogumglyniumOP2y ago
you're right, i wasn't (i added the async before void funcname), now its working perfectly Thanks guys!! what about getting the logs from the api console?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
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