我的 Hub 需要一个构造函数来连接到 DependencyInjections。
一旦我为我的 Hub 声明了一个 private readonly
依赖变量和一个构造函数 - 即使其中没有代码 - 我甚至在 OnConnectedAsync
之前就得到了 ConnectionClose
> 运行。
意味着 OnConnectedAsync
永远不会运行!
Error: Connection disconnected with error 'Error: Server returned an error on close: Connection closed with an error.'.
private readonly Chatter _chatter;
public ChatHub(Chatter chatter)
{
_chatter = chatter;
}
甚至
private readonly Chatter _chatter;
public ChatHub(Chatter chatter)
{
//Empty
}
但这没关系:
private readonly Chatter _chatter;
public ChatHub()
{
//Whatever...
}
最佳答案
您需要注册您的依赖项,以便它可以注入(inject)到 Hub 中。
services.AddTransient<Chatter>();
有可能 DI 容器不知道依赖项以便注入(inject)它,这会导致错误。
关于c# - 如果我为集线器定义构造函数,连接将关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662258/