Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to separate dependency configuration from UI project of an ASP.NET Core Blazor Web App?

$
0
0

I have a Blazor Web App with the Server hosting model in .NET 8 with the WebApplication.CreateBuilder minimal hosting model.

In the solution I use multiple library projects to foster a clean architecture throughout the application, most notably I control exactly how projects reference each other. This way it is technically not possible that, for example, the domain model project accidentally references a type from the persistence (database) project.

As with every default Blazor app, this Blazor project currently does two things:

  • Configuring the dependency injection of the whole app
  • Hosting the UI components

The first point causes the Blazor project to reference each other project of the solution. Which leads to the possibility that any UI component now can reference a type from, for example, the persistence project. My goal is to prevent that so that the Blazor components can only know about types in projects with interfaces for the UI.

Since configuring dependencies for injection has to be done very early in the hosting startup process, my idea was to create a dedicated project for the hosting which is allowed to know any other project to configure the dependency injection. Which means that the builder.Services.Add(...) part for my custom types wouldn't be in the Blazor project anymore.

How can I achieve that?

The hosting project surely needs some kind of Program.cs, optimally using top-level statements without the class Program boilerplate. But does it make sense to use WebApplication.CreateBuilder there? This seems to create trouble since the hosting project then must be of type <Project Sdk="Microsoft.NET.Sdk.Web"> and then the default behavior for ContentRootPath/WebRootPath doesn't work, since they are looked for in the hosting project, while actually being in the Blazor project. And maybe other problems exist too, which I haven't discovered yet.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>