我有一个继承自SignalR Hub类的类,并且正在覆盖OnConnected Task。当客户端连接时,OnConnected被触发,但我想使用EventHandler将其冒泡到Processor类。不幸的是,EventHanlder始终为null。有什么想法吗?
public class LoadBalancerHub : Hub
{
public event EventHandler ClientConnected;
public LoadBalancerHub() : base()
{
}
public override Task OnConnected()
{
ClientConnected?.Invoke(this, EventArgs.Empty);
return base.OnConnected();
}
}
public class Processor
{
public LoadBalancerHub Hub;
public Processor()
{
Hub = new LoadBalancerHub();
Hub.ClientConnected += Hub_ClientConnected;
}
private void Hub_ClientConnected(object sender, EventArgs e)
{
//do stuff here when event raised
}
}
答案 0 :(得分:0)
您的代码对我来说很合适。调试时,处理程序在此行中是否有效? Hub.ClientConnected + = Hub_ClientConnected;
如果您有多个LoadBalancerHub对象,并且没有订阅触发OnConnected()的对象,该怎么办...