"Scoped" 服务的构造函数被调用不止一次

"Scoped" service's constructor getting called more than once

我们有 Blazor 应用程序,我们在其中配置生命周期范围设置为 "Scoped" 的服务。出于某种原因,此服务的构造函数被调用了不止一次。有谁知道为什么?

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddScoped<IProjectClient, ProjectClient>();
}

ProjectClient 的构造函数被调用了不止一次。任何 thoughts/pointers?

是的,它被调用了两次,因为您正在使用预渲染。转到 _Host.cshtml 并将 render-mode="ServerPrerendered" 设置为 render-mode="Server",它只会被调用一次。