简体   繁体   中英

How to correctly publish an ASP.NET Core hosted Blazor app when having multiple projects?

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

  • Web Project (Microsoft.NET.Sdk.Web, Exe output type)
  • Blazor Project (Microsoft.NET.Sdk.BlazorWebAssembly, Exe output type)

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

So if I modify the project structure to include a Desktop UI with separation of concerns I might eventually end up with something like this:

  • UI 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 (eg ports, authentication, etc.)
    • References Web Project
  • Web 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 Blazor Project
  • Blazor 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 UI 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 UI Project also use the Microsoft.NET.Sdk.Web or you explicitly publish the Web Project into the same folder as the UI Project .

Both workarounds seem wrong to me, either you have to know to explicitly publish a project that is not a top level project or you need to modify your top level project to make all aspects of your referenced projects work.

That gets me to the question, what is the right way to correctly publish (web) applications in .NET?

edit : Edited the title and the modified project structure to hopefully make things more clear.

You probably need to structure the projects within your solution with the correct dependancies.

The diagrams below are lifted from one of my demo solutions to show one possible project structure. The arrows represent inter project dependancies. This design is based on Clean Design principals. I change the startup project to run in the different modes.

In this design your "Desktop App" or "Mobile App" will have a UI project (Blazr.Demo.DesktopUI), whatever other projects are needed to support the Desktop application, and a top level build project Blazr.Demo.Desktop...

This shows the Blazor Server project dependancies: 在此处输入图像描述

And this the WASM project dependancies. 在此处输入图像描述

My solution consists of nine projects, which may seem overkill, but organises the code logically. The projects enforce the design dependencies and separation of concern principles.

  1. Blazr.Demo.Config is a Razor Library project. It contains the application base code and Service configurations.
  2. Blazr.Demo.Data is a Razor Library project. It contains all the Data Domain shared code.
  3. Blazr.Demo.Core is a Razor Library project. It contains all the Core/Application Domain shared code.
  4. Blazr.Demo.UI is a Razor Library project. It contains all the UI Domain shared code.
  5. Blazr.Demo.Controllers is a Razor Library project. It contains all the API Controllers code.
  6. Blazr.Demo.WASM is a BlazorWebAssembly project. It builds the WASM code to deploy to the browser.
  7. Blazr.Demo.Server.Web is an AspNetCore Web project, configured to support the Blazor Server Hub, and contains the launch page for the Blazor Server SPA.
  8. Blazr.Demo.WASM.Web is an AspNetCore Web project configured to provide server side files for the WASM SPA, the launch page for the Blazor Server SPA, and any API controllers.
  9. Blazr.Demo.Tests is a XUnit Test project. It contains all the test code for the solution.

If this answer doesn't help, no problem, I'll delete it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM