I have an app that needs to have two separate web applications, listening on different ports, for different uses.
I feel like the way I’m currently doing it is messy, or possibly dangerous.
My current approach is:
- Have my main Program be a subclass of
IHostedService
- In my static
Main()
, create anIHostBuilder
and configure services with:services.AddHostedService<MyProgram>();
services.AddSingleton<WebApplication1>();
services.AddSingleton<WebApplication2>();
MyProgram
then gets instances of bothWebApplication1
andWebApplication2
- And then in
MyProgram.StartAsync()
, I call:_webApp1.Initialize();
_webApp2.Initialize();
await Task.WhenAll(_webApp1.Start(), _webApp2.Start());
- Where
Initialize()
creates the internalWebApplication
using aWebApplicationBuilder
Start()
starts the internal WebApplication running
I’ve had to jump through some DI hoops to get some instances shared between the two WebApplications, so I was hoping there might be a better way of doing this.