Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having exception while trying to send notifications to SignalR connected clients based on DeviceStateChangeEvent #69

Open
mehdiagharlou opened this issue Jan 24, 2020 · 0 comments

Comments

@mehdiagharlou
Copy link

mehdiagharlou commented Jan 24, 2020

I'm trying to push notifications to clients who have connected to SignalR Hub based on related events are happening in PBX VOIP server.

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.

using Microsoft.AspNetCore.SignalR;
using System;
using System.Threading.Tasks;
using AutoDialFinal.Models;
using AsterNET.ARI;
using AsterNET.ARI.Models;

namespace AutoDial.Hubs
{
    public class AgentHub : Hub
    {
        public AgentHub()
        {
            AriClient ActionClient;

            ActionClient = new AriClient(
                new StasisEndpoint("voip", 8088, "root", "password"),
                    "HelloWorld",
                    true);

            ActionClient.OnDeviceStateChangedEvent += ActionClientDeviceStateChangeEventAsync;

            ActionClient.Connect();

            void ActionClientDeviceStateChangeEventAsync(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()
                };

                SendNotificationToAgentAsync(notification);

            }
        }


        public override async Task OnConnectedAsync()
        {
            var notification = new DeviceStateChangeNotification
            {
                NotificationText = "Welcome!",
                SentAt = DateTime.Now.ToString()
            };

            await Clients.Caller.SendAsync(
                "ReceiveNotification",
                notification.NotificationText,
                notification.SentAt
            );

            await base.OnConnectedAsync();

        }

        public void SendNotificationToAgentAsync(DeviceStateChangeNotification notif)
        {
            Clients.Caller.SendAsync(
                "ReceiveNotification",
                notif.NotificationText,
                notif.SentAt
            );
        }
    }
}

Exception will raise in below method in ARIClient class while any device state change happening in PBX:

    private void _eventProducer_OnMessageReceived(object sender, MessageEventArgs e)
    {
        Debug.WriteLine(e.Message);
        // load the message
        var jsonMsg = (JObject)JToken.Parse(e.Message);
        var eventName = jsonMsg.SelectToken("type").Value<string>();
        var type = Type.GetType("AsterNET.ARI.Models." + eventName + "Event");
        var evnt =
            (type != null)
                ? (Event)JsonConvert.DeserializeObject(e.Message, type)
                : (Event)JsonConvert.DeserializeObject(e.Message, typeof(Event));

        lock (_syncRoot)
        {
            if (_dispatcher == null)
                return;

            _dispatcher.QueueAction(() =>
            {
                try
                {
                    FireEvent(evnt.Type, evnt, this);
                }
                catch(Exception ex)
                {

                    **// HERE IS WERE EXCEPTION IS RAISING**

                    // Handle any exceptions that were thrown by the invoked event handler
                    if (!UnhandledException(this, ex))
                    {
                        Console.WriteLine("The event listener " + evnt.Type.ToString() + " cause an exeption: " + ex.Message);
                    }
                }
            });
        }
    }

How do I fix this problem?
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant