How to integrate ASP .NET project in a WPF app?
I need help with integrating a ASP.NET server into an executable WPF app. The goal is to only have a single executable, and when it runs, it opens up a WPF UI and spins up a local server which serves some HTML and api endpoints (this is all local).
Right now, i have 2 seperate projects: 1 is the ASP .NET which handles API endpoints and serves HTML, 2 is the WPF im building.
I'm trying to somehow also run the ASP .NET server when the WPF is opened.
I'm pratically brand new to C# .NET.
38 Replies
Reference the ASP project in your WPF project and run its main method
That should suffice
I keep getting this "Failed to bind to address <url> address already in use"
This is eveyrthing in my Program.cs file in the asp .net project
and then in WPF, i just run Main()
Try changing the port
Could be it's taken by something else
Do i need to change the "Output type" in the project setting or something? Right now, asp .net is a console app and the wpf is a window application
The error disappeared but when i run it's just not doing anything. When i change the wpf's app to be a console app, it shows this console, but it doesn't actually start the server
There seems to be some more info here: https://stackoverflow.com/questions/67674514/hosting-an-asp-web-api-inside-a-wpf-app-wont-stop-gracefully
Honestly not entirely sure how I'd do that
This worked for me
@🎄Fizbanbuzz 🎄@ZZZZZZZZZZZZZZZZZZZZZZZZZI finally figured out why i couldnt get it to work no matter what
I didn't know I needed a launchSettings.json in my WPF app
I think because it needs to do some redirect stuff
I mean I didn't have that issue, I sent you my example, I started with a wpf project
My example used di with wpf which is non standard
@Kowalski if you want me to walk through my example let me know
@🎄Fizbanbuzz 🎄No no the example you gave me worked, in the sense that if i go to the address, it will responds with Hello World
But i already had an entire ASP .NET with React that i built seperately and i didnt know how to integrate that into my new WPF app
OIC, yeah my example was pretty simple, mostly because of didn't want to build out controllers and all that, does the same principle fail with controllers?
I think it'd still work, i just need to do all the configurations in the asp.net project and have a method to start the server and use that in the WPF project. Something like this:
ASPNET:
WPF:
The problem is, i think, it's doing some proxy server stuff that i dont understand, and since i didnt have the launchSettings.json file in the WPF project, when i go to the address it doesn't do anything
I got it to display the react app properly now, but now im running into some weird problems with the api, again because of the proxy stuff
Do you have a repo? Or is this private stuff?
i can publish it, i dont mind
Take your time I'm watching movies with the wife and kids
đź‘Ť
GitHub
GitHub - kohwahskee/link-one
Contribute to kohwahskee/link-one development by creating an account on GitHub.
looking over, I think you might need to set up some details with kestrel, you can review docs here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-8.0#configure-endpoints I'm going to keep looking a bit, but it's late for me right now, so it might be tomorrow.
Oh..my...
I got it to work, but i have to copy the Controllers folder from my asp.net to my WPF
Almost as if it doesn't recognize controllers when i start the server from my WPF app is so confusing
Is the website always going to host from the wpf app?
Copying the controllers sounds weird, but it's been a long week
GitHub
GitHub - devdevdeveau/wpf
Contribute to devdevdeveau/wpf development by creating an account on GitHub.
nothing super exciting here, pretty much what I already posted
I experimented with zipping the resources/wwwroot directory and unpacking that pretty naively, it might even break xplat
nm
wpf
we're already out of xplat
I know it's not quite the same scenario, but maybe it helps some
I feel like im just glossing over some weird configurations. It has to be way simpler than that
At this point im pretty positive it has somethign to do with asp.net url and path it will always be hosted locally from the WPF
@🎄Fizbanbuzz 🎄 Okay i figured it out. Apparently if i wanna use controllers from a different assembly, i need to add them as "ApplicationParts" like this
TIL!
The last time I messed with that sorta thing it was in old school aspnet webapi, not to be confused with aspnet core.
Hi, I've got similar questions.
my project at https://github.com/laipuran/Gear
GitHub
GitHub - laipuran/Gear: A PPT classifier tool for class and a tool ...
A PPT classifier tool for class and a tool for notifying. - GitHub - laipuran/Gear: A PPT classifier tool for class and a tool for notifying.
The problem is that I changed the original Main method to a new method, I call it in the WPF app.xaml.cs 's ApplicationStartup
The GUI started but got stuck and the server does not work
Anyone can help?
@BallBuster McCheese Hi, I've noticed that you once discussed this last year. Would you mind helpin me qwq
thanks a lot
I looked at your repo I took a different approach; you're welcome to look at my approach here https://github.com/devdevdeveau/wpf.
okay i 'll check it right away
Honestly I looked at the initial question as an academic exercise. I've no practical experience in the topic.
My approach differs from yours in that I'm just spinning up the server inside my wpf app.
got it
Honestly I barely remember writing the code.
I changed the default method to the one you use, and it doesn't stuck anymore
It's just about changing the method used, problem solved thx