0

Is there a way to host 2 blazor server apps in one ASP Core project, so that I have one executable/process, which exposes the two different apps on different ports?

I know, that I can restrict routes via .RequireHost("*:8080") to a specific port, but I can't get my head around, how to do this with two blazor server side rendering apps.

Thanks in advance!

joa77
  • 85
  • 1
  • 9
  • Why would you want this? What is wrong with having two seperate blazor projects in the same solution? – klekmek Sep 09 '22 at 08:49
  • It's a local network only project (a quizmaster (one blazor server) and multiple quiz attendees (the other blazor server app)) and I need the direct communication between the projects bei shared classes. I know, that there are probably better ways to do this, but for this specific case it's necessary and easier to have it like this. – joa77 Sep 09 '22 at 08:53
  • This makes no sense to me. Why do you want to use ports, rather than routing or roles? – Bennyboy1973 Sep 09 '22 at 08:55
  • It's some sort of security to prevent the quiz attendees to enter the master view. Without the overhead which comes with authentication. – joa77 Sep 09 '22 at 08:57
  • Why don't you create one Blazor project with 2 endpoints and communicate through those? – klekmek Sep 09 '22 at 08:57
  • 2 Endpoints would be great, but I haven't figured out, how to map these to different ports... – joa77 Sep 09 '22 at 08:58
  • Does [this](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-6.0#host-matching-in-routes-with-requirehost) help you? In your case it would be `[Host("*:8080")]` – klekmek Sep 09 '22 at 08:59
  • @klekmek unfortunately, because I dont't know how to apply this to blazor apps. With "normal" API or MVC apps it's works without problems. Or can I apply this attribute to the `@page "\"` routing in placer? – joa77 Sep 09 '22 at 09:01
  • If you are really keen on running two applications on two ports, you will need to create two blazor projects. If they are in the same solution as your `.Shared` project, you can still reuse those classes. – klekmek Sep 09 '22 at 09:04
  • @klekmek it would be perfectly fine, when it's just one project, but with port restricted access to pages – joa77 Sep 09 '22 at 09:08
  • The port you use is controlled by the Production web server you're running on. You don't normally configure a port in the application code - just in the configuation for the endpoint. So if on the production server both ports point to the same site, it's up to your code to differentiate what to display based on port - probably usinf different App components. – MrC aka Shaun Curtis Sep 09 '22 at 09:31
  • If you want to using different sites in the same solution - https://stackoverflow.com/questions/71104254/create-a-multiple-webassembly-projects-in-a-single-solution – MrC aka Shaun Curtis Sep 09 '22 at 09:39

1 Answers1

3

So I figured it out:

In __host.cshtml use the following code to decide, which app should be loaded:

@switch(HttpContext.Request.Host.Port)
{
    case 1234:
        <component type="typeof(App1)" render-mode="ServerPrerendered"/>
        break;
    case 4321:
        <component type="typeof(App2)" render-mode="ServerPrerendered"/>
        break;
}
joa77
  • 85
  • 1
  • 9
  • I'd probably set the port numbers in appsettings so you can easily configure them on the production server, but agree as per my comment on the question. – MrC aka Shaun Curtis Sep 09 '22 at 09:34
  • Why do you have to inject `IHttpContextAccessor ?` Can't you access `HttpContext` directly ? – enet Sep 09 '22 at 09:45
  • Yes, setting the port number via settings is the better option. I provided this just as a small example. It even works with outsourcing the Apps to Razor class libraries. @enet that's true, of course you can use the HttpContext directly... – joa77 Sep 09 '22 at 10:12