locked
dependency_OnChange not fire up at all? RRS feed

  • Question

  • User-1045082127 posted

    hi every body .

    i use this script to enable service broker:

    ALTER DATABASE testDB SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE ;

    and from application side this is a MessagesRepository class which contains :

    public class MessagesRepository
        {
            readonly string _connString =
            ConfigurationManager.ConnectionStrings["Context"].ConnectionString;
    
            public IEnumerable<tbTest> GetAllMessages()
            {
                var messages = new List<tbTest>();
                using (var connection = new SqlConnection(_connString))
                {
                    connection.Open();
                    using (var command = new SqlCommand(@"SELECT Id,Name from tbTest", connection))
                    {
                        command.Notification = null;
    
                        var dependency = new SqlDependency(command);
                        dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                        if (connection.State == ConnectionState.Closed)
                            connection.Open();
    
                        var reader = command.ExecuteReader();
    
                        while (reader.Read())
                        {
                            messages.Add(item: new tbTest
                            {
                                Id = (int)reader["Id"],
                                Name = (string)reader["Name"],
                            });
                        }
                    }
                }
                return messages;
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {
                if (e.Type == SqlNotificationType.Change)
                {
                    HubTest.SendMessages();
                }
            }
        }

    and also enabled in a global.ascx application start

                SqlDependency.Start(ConfigurationManager.ConnectionStrings["Context"].ConnectionString);
    

    and application end.

                SqlDependency.Stop(ConfigurationManager.ConnectionStrings["Context"].ConnectionString);
    

    but dependency_OnChange cant be fire up plz help me?

    Friday, September 28, 2018 2:25 PM

Answers

  • User-1045082127 posted

    here is a problem:

    SELECT Id,Name from tbTest

    it must be dbo.tbTest

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 29, 2018 7:31 AM