I'm trying to push notifications to clients who have connected to SignalR Hub based on related events are happening in Asterisk PBX VOIP server using AsterNet ARI.
I can get events using DeviceStateChangedEvent class from AsterNet using an event handler and want to push notification to clients related to their devices' state changes.
Also, SignalR connection is working as well and welcome message is showing on client web page.
But the problem is while sending notification by SendAsync method to caller client, my Hub goes to be disposed and below exception raised:
System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'AgentHub'.'
Here is my Hub class which I've overridden the OnConnectedAsync() method to send welcome message and put event handler for listening events from PBX.
public class AgentHub : Hub
{
public static AriClient ActionClient;
public override async Task OnConnectedAsync()
{
ActionClient = new AriClient(
new StasisEndpoint("voipserver", port, "username", "password"),
"HelloWorld",
true);
ActionClient.Connect();
ActionClient.OnDeviceStateChangedEvent += new DeviceStateChangedEventHandler(async delegate (IAriClient sender, DeviceStateChangedEvent e)
{
var notification = new DeviceStateChangeNotification
{
NotificationText = e.Device_state.Name + "'s state changed to " + e.Device_state.State,
SentAt = DateTime.Now.ToString()
};
await Clients.Caller.SendAsync(
"ReceiveNotification",
notification.NotificationText,
notification.SentAt
);
});
var notification = new DeviceStateChangeNotification
{
NotificationText = "Welcome!",
SentAt = DateTime.Now.ToString()
};
await Clients.Caller.SendAsync(
"ReceiveNotification",
notification.NotificationText,
notification.SentAt
);
await base.OnConnectedAsync();
}
}
ActionClient
because it's static. TheOnConnectedAsyc
is called for each user that connects and becauseActionClient
is static, it will get overwritten each time. You should probably have a single event handler, maybe subscribed in your hub's constructor, and have the handler send toClients.All
. – Jeff Shepler