SignalR,根据事件发生向连接的客户端推送通知使集线器被处置

问题描述 投票:0回答:1

我正在基于Asterisk使用AsterNet ARI在PBX VOIP服务器中基于相关事件将通知推送到已连接到SignalR Hub的客户端。

我可以使用事件处理程序从AsterNet使用DeviceStateChangedEvent类获取事件,并希望将通知推送到与设备状态更改相关的客户端。

此外,SignalR连接也正常工作,并且欢迎消息在客户端网页上显示。

但是问题是,当通过SendAsync方法向呼叫者客户端发送通知时,我的集线器将被处置,并在引发以下异常的情况下:

System.ObjectDisposedException:'无法访问已处置的对象。对象名称:“ AgentHub”。]]

这是我的Hub类,我重写了OnConnectedAsync()方法来发送欢迎消息并放置事件处理程序以侦听来自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();

        }
    }

我正在尝试使用AsterNet ARI在Asterisk PBX VOIP服务器中基于相关事件将通知推送到已连接到SignalR Hub的客户端。我可以使用...

c# asp.net-core-signalr asterisk-ari
1个回答
0
投票

集线器是短暂的,它们仅在执行集线器方法时存在。这里发生的是您正在为OnDeviceStateChangedEvent创建一个事件,该事件捕获“ this”(集线器)。稍后,当事件被触发时,您将尝试访问方法退出后立即处置的集线器。

© www.soinside.com 2019 - 2024. All rights reserved.