Warm tip: This article is reproduced from serverfault.com, please click

Blazor WebAssembly App in .NET 5.0 with MatBlazor 2.8.0

发布于 2020-11-27 23:39:51

I have followed the installation instructions from https://www.matblazor.com/.

1.Add @using MatBlazor in main _Imports.razor

@using MatBlazor

2.Add script section to index.html (head section)

<script src="_content/MatBlazor/dist/matBlazor.js"></script>
<link href="_content/MatBlazor/dist/matBlazor.css" rel="stylesheet" />

3.Add registration of service to Startup.cs

services.AddMatBlazor();

4.Add MatPortalHost component to root component (App.razor) for some services like MatDialogService, MatPortalService

<MatPortalHost></MatPortalHost>

Error when starting:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property 'PortalService' on type 'MatBlazor.MatPortalHost'. There is no registered service of type 'MatBlazor.IMatPortalService'. System.InvalidOperationException: Cannot provide a value for property 'PortalService' on type 'MatBlazor.MatPortalHost'. There is no registered service of type 'MatBlazor.IMatPortalService'.

Questioner
Ogglas
Viewed
11
Ogglas 2020-11-28 07:39:51

Turns out 3.Add registration of service to Startup.cs is not the same for Blazor WebAssembly App.

Add builder.Services.AddMatBlazor(); to Program.cs Main method in the BlazorWebAssemblyApp.Client and then everything should work. No need to edit anything in BlazorWebAssemblyApp.Server or BlazorWebAssemblyApp.Shared.