首页 > 解决方案 > 在类库中使用 AddServerSideBlazor()

问题描述

我正在尝试像这样设置我自己的服务集合:

public static void AddMyServices(this IServiceCollection services)
{
    services.AddServerSideBlazor();
    //services.AddTransient<IMyService, MyService>();
}

我不知道如何让它运行,我尝试添加几个 NuGet 引用,但没有一个起作用。例如Microsoft.AspNetCore.Components.Web开始AddServerSideBlazor();工作。

我想在我的 Startup.cs 中这样使用它:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMyServices();
}

我得到的错误:

“IServiceCollection”不包含“AddServerSideBlazor”的定义,并且找不到接受“IServiceCollection”类型的第一个参数的可访问扩展方法“AddServerSideBlazor”(您是否缺少 using 指令或程序集引用?)

编辑:经过更多研究后,我认为正确的包是 Microsoft.AspNetCore.App.Ref但通过 NuGet 添加它会给我这个错误:

Microsoft.AspNetCore.App.Ref 5.0.0 包的包类型 DotnetPlatform 与此项目不兼容。

标签: c#asp.net-mvcasp.net-coreblazor

解决方案


您将需要扩展 IServiceCollection。

public static Microsoft.Extensions.DependencyInjection.IServerSideBlazorBuilder AddServerSideBlazor (this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Action<Microsoft.AspNetCore.Components.Server.CircuitOptions>? configure = default);

找到https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.componentservicecollectionextensions.addserversideblazor?view=aspnetcore-5.0


推荐阅读