【发布时间】:2020-09-17 22:04:05
【问题描述】:
我正在创建 Blazor 项目,起初一切正常,直到我需要将 IJSRuntime 注入 cs 文件。
Microsoft.JSInterop;
...
...
public BaseService(IJSRuntime jSRuntime)
{
}
BaseService 被另一个名为AuthenticationServices 的服务继承,该服务也使用名为IAuthentication 的接口。因此
using Microsoft.JSInterop;
public class AuthenticationServices : BaseService, IAuthentication
{
public AuthenticationServices(IJSRuntime jSRuntime) : base(jSRuntime)
{
}
}
我的问题出在Startup.csfile 中
services.AddSingleton<IAuthentication, AuthenticationServices>();
如果我运行它说的应用程序,
InvalidOperationException: Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton '...IAuthentication'
这是什么意思?我是否正确地只需要添加一些东西?
【问题讨论】: