C
C#•13mo ago
kohwahskee

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
Angius
Angius•13mo ago
Reference the ASP project in your WPF project and run its main method That should suffice
kohwahskee
kohwahskeeOP•13mo ago
I keep getting this "Failed to bind to address <url> address already in use"
kohwahskee
kohwahskeeOP•13mo ago
This is eveyrthing in my Program.cs file in the asp .net project
No description
kohwahskee
kohwahskeeOP•13mo ago
and then in WPF, i just run Main()
kohwahskee
kohwahskeeOP•13mo ago
No description
Angius
Angius•13mo ago
Try changing the port Could be it's taken by something else
kohwahskee
kohwahskeeOP•13mo ago
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
kohwahskee
kohwahskeeOP•13mo ago
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
No description
Angius
Angius•13mo ago
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
Mayor McCheese
Mayor McCheese•13mo ago
This worked for me
public partial class App : Application
{
public IServiceProvider ServiceProvider { get; }
private readonly IServiceScope? _serviceScope;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly WebApplication _host;

public App()
{
_cancellationTokenSource = new CancellationTokenSource();

var builder = WebApplication.CreateBuilder();
builder.Services.AddSingleton<MainWindow>();
builder.Services.AddControllersWithViews();

_host = builder.Build();

_host.UseHttpsRedirection();
_host.MapGet("/", () => "Hello World");

_serviceScope = _host.Services.CreateScope();
ServiceProvider = _serviceScope.ServiceProvider;
}

protected override async void OnStartup(StartupEventArgs e)
{
await _host.StartAsync(_cancellationTokenSource.Token);

var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();

base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
_serviceScope?.Dispose();
await _host.StopAsync(_cancellationTokenSource.Token);
await _host.DisposeAsync();
base.OnExit(e);
}
}
public partial class App : Application
{
public IServiceProvider ServiceProvider { get; }
private readonly IServiceScope? _serviceScope;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly WebApplication _host;

public App()
{
_cancellationTokenSource = new CancellationTokenSource();

var builder = WebApplication.CreateBuilder();
builder.Services.AddSingleton<MainWindow>();
builder.Services.AddControllersWithViews();

_host = builder.Build();

_host.UseHttpsRedirection();
_host.MapGet("/", () => "Hello World");

_serviceScope = _host.Services.CreateScope();
ServiceProvider = _serviceScope.ServiceProvider;
}

protected override async void OnStartup(StartupEventArgs e)
{
await _host.StartAsync(_cancellationTokenSource.Token);

var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();

base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
_serviceScope?.Dispose();
await _host.StopAsync(_cancellationTokenSource.Token);
await _host.DisposeAsync();
base.OnExit(e);
}
}
<Application x:Class="WpfWithAspNetCore.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfWithAspNetCore">
<Application.Resources>

</Application.Resources>
</Application>
<Application x:Class="WpfWithAspNetCore.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfWithAspNetCore">
<Application.Resources>

</Application.Resources>
</Application>
kohwahskee
kohwahskeeOP•13mo ago
@🎄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
Mayor McCheese
Mayor McCheese•13mo ago
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
kohwahskee
kohwahskeeOP•13mo ago
@🎄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
Mayor McCheese
Mayor McCheese•13mo ago
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?
kohwahskee
kohwahskeeOP•13mo ago
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:
public static class AspNetServer
{
public static WebApplication CreateWebApplication()
{
var builder = WebApplication.CreateBuilder();

builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<Automator>();

var app = builder.Build();

app.UseHttpsRedirection();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();

app.MapFallbackToFile("index.html");
app.MapControllers();

return app;
}

public static void StartServer()
{
CreateWebApplication().Run();
}
}
public static class AspNetServer
{
public static WebApplication CreateWebApplication()
{
var builder = WebApplication.CreateBuilder();

builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<Automator>();

var app = builder.Build();

app.UseHttpsRedirection();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();

app.MapFallbackToFile("index.html");
app.MapControllers();

return app;
}

public static void StartServer()
{
CreateWebApplication().Run();
}
}
WPF:
protected override async void OnStartup(StartupEventArgs e)
{
AspNetServer.StartServer();
var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}
protected override async void OnStartup(StartupEventArgs e)
{
AspNetServer.StartServer();
var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}
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 suffer
Mayor McCheese
Mayor McCheese•13mo ago
Do you have a repo? Or is this private stuff?
kohwahskee
kohwahskeeOP•13mo ago
i can publish it, i dont mind
Mayor McCheese
Mayor McCheese•13mo ago
Take your time I'm watching movies with the wife and kids
kohwahskee
kohwahskeeOP•13mo ago
đź‘Ť
kohwahskee
kohwahskeeOP•13mo ago
GitHub
GitHub - kohwahskee/link-one
Contribute to kohwahskee/link-one development by creating an account on GitHub.
Mayor McCheese
Mayor McCheese•13mo ago
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.
kohwahskee
kohwahskeeOP•13mo ago
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 NotLikeThis is so confusing
Mayor McCheese
Mayor McCheese•13mo ago
Is the website always going to host from the wpf app? Copying the controllers sounds weird, but it's been a long week
Mayor McCheese
Mayor McCheese•13mo ago
GitHub
GitHub - devdevdeveau/wpf
Contribute to devdevdeveau/wpf development by creating an account on GitHub.
Mayor McCheese
Mayor McCheese•13mo ago
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
kohwahskee
kohwahskeeOP•13mo ago
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 NotLikeThis it will always be hosted locally from the WPF
kohwahskee
kohwahskeeOP•13mo ago
@🎄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
No description
Mayor McCheese
Mayor McCheese•13mo ago
TIL! The last time I messed with that sorta thing it was in old school aspnet webapi, not to be confused with aspnet core.
Puran Lai
Puran Lai•10mo ago
Hi, I've got similar questions.
Puran Lai
Puran Lai•10mo ago
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.
Puran Lai
Puran Lai•10mo ago
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
Mayor McCheese
Mayor McCheese•10mo ago
I looked at your repo I took a different approach; you're welcome to look at my approach here https://github.com/devdevdeveau/wpf.
Puran Lai
Puran Lai•10mo ago
okay i 'll check it right away
Mayor McCheese
Mayor McCheese•10mo ago
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.
Puran Lai
Puran Lai•10mo ago
got it
Mayor McCheese
Mayor McCheese•10mo ago
Honestly I barely remember writing the code.
Puran Lai
Puran Lai•10mo ago
I changed the default method to the one you use, and it doesn't stuck anymore
No description
Puran Lai
Puran Lai•10mo ago
It's just about changing the method used, problem solved thx
No description
Want results from more Discord servers?
Add your server