Blazor WebAssembly:无法为属性“AuthenticationStateProvider”提供值

4
我有一个Blazor WebAssembly项目,其中包含用于身份验证的服务器端API。我已经将Identity添加到这个服务器项目中,并且似乎运行良好。注册和登录都可以正常工作。
但是当我将标记添加到我的WebAssembly应用程序的App.razor文件中时,Webassembly应用程序会崩溃并显示错误:无法为Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState类型上的属性“AuthenticationStateProvider”提供值。 我看到很多关于此的帖子,每次解决方案似乎都是在Program.cs文件中添加builder.Services.AddAuthorizationCore(),但这并没有解决任何问题。
由于这是在我自己添加Identity之前存在的WebAssembly项目,我怀疑缺少某些内容。你能否想到我可能需要检查什么?
我的Program类:
public static async Task Main(string[] args)
{
   var builder = WebAssemblyHostBuilder.CreateDefault(args);
   builder.RootComponents.Add<App>("#app");

   builder.Services.AddAuthorizationCore();
   builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

   await builder.Build().RunAsync();
}

我的App.razor文件:

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>
</CascadingAuthenticationState>
1个回答

8

1
元答案是,为了使 DI 正常工作,您必须首先在启动时注册服务。 - Zimano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接