ServiceBrokerListener icon indicating copy to clipboard operation
ServiceBrokerListener copied to clipboard

How to use it with SignalR?

Open srbhklkrn opened this issue 6 years ago • 1 comments

I'm trying to use your library and push changes using signalR but it's not triggering the changes any idea why?

getting following error

System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.

on line _sqlDependency.Start();

public class SignalServer : Hub
    {
        public readonly TestController _testController;
        public IConfiguration Configuration { get; }
        public SqlDependencyEx _sqlDependency;

        public SignalServer(TestController testController, IConfiguration configuration)
        {
            Configuration = configuration;
            _testController = testController;

            SqlDependencyEx _sqlDependency = new SqlDependencyEx(Configuration.GetConnectionString("DefaultConnection"), "PCFStatus", "apps");
        }

        public async void NotifyConnection()
        {
            await Clients.All.SendAsync("TestBrodcasting", _testController.GetAllApps());

             _sqlDependency.Start();

            if (_sqlDependency != null)
            {
                _sqlDependency.TableChanged += OnDataChange;
               
            }
        }

        private async void OnDataChange(object sender, SqlDependencyEx.TableChangedEventArgs e)
        {
            await Clients.All.SendAsync("TestBrodcasting", _testController.GetAllApps());
        }

    }

srbhklkrn avatar Aug 28 '19 20:08 srbhklkrn

hubs are transients, so every time the methods are invoked, it creates a new sqldependency.

https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-5.0#:~:text=Hubs%20are%20transient%3A,on%20the%20hub%20staying%20alive.

weirdyang avatar May 30 '21 16:05 weirdyang