-2

When I create an ASP.NET Core hosted Blazor app from the .NET template I get something like this:

  • WebHost project (Microsoft.NET.Sdk.Web, Exe output type)
  • BlazorApp project (Microsoft.NET.Sdk.BlazorWebAssembly, Exe output type)

I can easily run dotnet publish on the WebHost project and will get an executable that hosts all the content of the Blazor web app.

So if I want to add a server configuration UI e.g. using WPF, I eventually end up with something like this. (the WebHost project is now a library and used/referenced by ServerUI)

  • ServerUI project
    • Uses Microsoft.NET.Sdk with UseWPF set to true
    • OutputType is set to WinExe
    • Top level project which es meant to be the entry point for the (server) application
    • Contains a graphical user interface to set up the server (e.g. ports, authentication, etc.)
    • References WebHost project
  • WebHost project
    • Uses Microsoft.NET.Sdk.Web to have access to the ASP.NET Core runtime
    • OutputType is set to Library
    • Contains all the code for ASP.NET Core hosting
    • References BlazorApp project
  • BlazorApp project
    • Uses Microsoft.NET.Sdk.BlazorWebAssembly
    • OutputType is set to Exe (default)
    • Contains all the Blazor web app stuff to run in the browser

If I now run dotnet publish on the ServerUI project, I will end up with an executable that shows a user interface and has ASP.NET Core hosting stuff but accessing the Blazor web app will fail, since the wwwroot folder will not be published that way.

Now there are two ways to fix this issue that I am aware of:

  • Either you make the ServerUI also use the Microsoft.NET.Sdk.Web
  • or you explicitly publish either the WebHost or the BlazorApp project into the same folder as the ServerUI project.

That gets me to the question, is it actually possible to use Microsoft.NET.Sdk.Web for library projects and if so, what are the limitations.

Steps to reproduce

  • Clone this repo
  • Open a terminal in the checked out folder
  • Run dotnet publish ServerUI -o bin\publish
  • wwwroot folder will be missing in the bin\publish directory
  • In contrast running dotnet run --project ServerUI will nevertheless work as intended, since it does not rely on the publish behavior
Michael Dietrich
  • 451
  • 1
  • 5
  • 17
  • 2
    This doesn't make sense. The `Web` SDK is supposed to output an application, not a library. I don't see how this is a (correct) separation of concerns. – Camilo Terevinto Jan 18 '22 at 09:45
  • You're mixing web and desktop in a single project? – Camilo Terevinto Jan 18 '22 at 09:58
  • The simplified project structure is described in the main post. Top level project is a project with a WPF-GUI that references a project handling the ASP.NET stuff. So no, it is not mixed. It sounded like that you suggested that the top level project must use the Web SDK or I misunderstood you. That's why I pleased you to get a bit more into detail to make things clear. What else would be best practice to structure this? – Michael Dietrich Jan 18 '22 at 10:12
  • Can you [edit] this post and make it explicit which project references which and the reasons? It sounds like you are not handling dependencies correctly but it's hard to say with the current information. Do you want to host the ASP.NET Core app inside the WPF app? – Camilo Terevinto Jan 18 '22 at 10:21
  • Thank you. As of now I thought I usually publish only top level projects, since (transitively) depending projects are automatically part of it. Maybe I have a misunderstanding of how publishing applications is meant to work. So when do I know I need to publish a project? But if understand you correctly and the WASM project is a separate project graph in that manner, why does it work if I only publish the Web Project from the ASP.NET template? – Michael Dietrich Jan 18 '22 at 12:42
  • The [documentation](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish#name) of `dotnet publish` states: _Publishes the application and its dependencies to a folder for deployment to a hosting system._ So all projects are dependencies of **UI Project**. Could you point to some post or alike that clarifies, why it does not work here that way and more than one publish is needed? – Michael Dietrich Jan 18 '22 at 14:45
  • I don't see why you say there is no relation between Desktop and Server project. The relationship as stated is A > B > C. Just normal ProjectReferences. I will add a link to a git repo. Describing the project graph by text was perhaps a bad choice. – Michael Dietrich Jan 18 '22 at 22:40
  • @MichaelDietrich - I've updated my answer based on the GitHub repo. – MrC aka Shaun Curtis Jan 19 '22 at 14:15
  • You still have *two separate apps*. Your WPF app should not *depend* on the web host project, because they're two different apps. If they have common dependencies, those should be in a separate class library that they can both reference. You should publish twice. It seems you're mixing up logical dependencies between apps (like a client/server model) and actual code dependencies (needing to use classes from one project or the other). Those are separate concepts. – mason Jan 19 '22 at 14:23
  • OK thanks. What exactly defines the border of an app? Is it tied to the used SDK and those different SDKs should not be mixed. E.g. normal SDK should not reference Web SDK? Neither are there any warnings nor I found any documentation for this. Do you have something you could point me to? As for what I think is the Web SDK is mainly for being used for top level projects and in that manner the ServerUI project should not have a ProjectReference to the WebHost project but rather start the WebHost as a second executable. Still I think it's a legit question and I don't understand the downvotes. – Michael Dietrich Jan 20 '22 at 10:46

1 Answers1

2

When you publish the Desktop project and run it separately, you're outside the VS development environment. There's no implicit way I know of to copy the wwwroot folder of the WASM project to your desktop project.

This is what I did:

  1. Set up a receiving folder for your application.
  2. Publish the ServerUI to the folder.
  3. Publish the BlazorApp to the same folder.

You need to fix the css in index.html.

    <link href="css/app.css" rel="stylesheet" />
    <link href="BlazorApp.styles.css" rel="stylesheet" />

You can then run the app from the published directory.

enter image description here

enter image description here

MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
  • @mason. No I used the same "answer", but totally revised it! – MrC aka Shaun Curtis Jan 19 '22 at 22:06
  • Thank you, I deleted my previous comment, since it was no longer fitting your revised answer. This is more or less the second workaround I mentioned in my post and I wondered if it is meant to be that way. As for what I understand using the Web SDK marks a new application and so needs its own publish and should better be built as an executable rather than a library. – Michael Dietrich Jan 20 '22 at 10:56
  • 1
    I've re-read your question now so yes agree this is your second workaround. As answerer's we often miss important nuggets in the question! I think the issue is that the WASM project gets compiles in one way and the Server project in another. You just need to put together some scripts that pull it all together. – MrC aka Shaun Curtis Jan 20 '22 at 15:45