SignalR 核心集线器连接 lambda 操作
SignalR core hubconnection lambda action
我有一个简单的 SignalR 核心集线器连接,集线器连接有一个 On
方法,它采用一个动作处理程序我目前有一个像下面这样的 lambda,但我想使用一个事件处理程序,所以我可以轻松取消订阅并防止任何内存泄漏。
hubConnection.On<string, string>(ReceiveMethodKey, (user, message) =>
{
var finalMessage = $"{user} says {message}";
// Update the UI
});
使用委托:
class MyClass : IDisposable
{
private Action<string, string> HubConnectionOnDelegate;
private void InitOrSomething()
{
//Pointer to a method, anonymous method, whatever...
HubConnectionOnDelegate = HubConnection_On;
}
private static void HubConnection_On(string user, string message)
{
var finalMessage = $"{user} says {message}";
// Update the UI
}
private void Elsewhere()
{
hubConnection.On<string, string>(ReceiveMethodKey, HubConnectionOnDelegate);
}
public void Dispose()
{
HubConnectionOnDelegate = null;
}
}
我有一个简单的 SignalR 核心集线器连接,集线器连接有一个 On
方法,它采用一个动作处理程序我目前有一个像下面这样的 lambda,但我想使用一个事件处理程序,所以我可以轻松取消订阅并防止任何内存泄漏。
hubConnection.On<string, string>(ReceiveMethodKey, (user, message) =>
{
var finalMessage = $"{user} says {message}";
// Update the UI
});
使用委托:
class MyClass : IDisposable
{
private Action<string, string> HubConnectionOnDelegate;
private void InitOrSomething()
{
//Pointer to a method, anonymous method, whatever...
HubConnectionOnDelegate = HubConnection_On;
}
private static void HubConnection_On(string user, string message)
{
var finalMessage = $"{user} says {message}";
// Update the UI
}
private void Elsewhere()
{
hubConnection.On<string, string>(ReceiveMethodKey, HubConnectionOnDelegate);
}
public void Dispose()
{
HubConnectionOnDelegate = null;
}
}