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

How to use it with SignalR? #66

Open
srbhklkrn opened this issue Aug 28, 2019 · 1 comment
Open

How to use it with SignalR? #66

srbhklkrn opened this issue Aug 28, 2019 · 1 comment

Comments

@srbhklkrn
Copy link

srbhklkrn commented Aug 28, 2019

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

    }
@weirdyang
Copy link

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.

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

2 participants