SqlDependency not working

I am trying SqlDepenedency for the first time. I do not receive any database update notifications.

I set a breakpoint inside:   OnChange(object sender, SqlNotificationEventArgs e),

but he never hits.

Here is my code:

    protected void Page_Load(object sender, EventArgs e)
    {

        Label1.Text = "Cache Refresh: " + DateTime.Now.ToLongTimeString();
        DateTime.Now.ToLongTimeString();
        // Create a dependency connection to the database.
        SqlDependency.Start(GetConnectionString());

        using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {
            using (SqlCommand command = new SqlCommand(GetSQL(), connection))
            {

                SqlDependency dependency =
                        new SqlDependency(command);

                    // Refresh the cache after the number of minutes
                    // listed below if a change does not occur.
                    // This value could be stored in a configuration file.
                                  connection.Open();

                dgHomeRequests.DataSource = command.ExecuteReader();
                    dgHomeRequests.DataBind();

            }
        }   
    }


    private string GetConnectionString()
    {
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file.
        //return "Data Source=(local);Integrated Security=true;" +"Initial Catalog=AdventureWorks;";
        return ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
    }
    private string GetSQL()
    {
        return "Select [Address] From [UserAccount1]";
    }


    void OnChange(object sender, SqlNotificationEventArgs e)
    {
        // have breakpoint here:
        SqlDependency dependency = sender as SqlDependency;

        // Notices are only a one shot deal
        // so remove the existing one so a new 
        // one can be added

        dependency.OnChange -= OnChange;

        // Fire the event
       /* if (OnNewMessage != null)
        {
            OnNewMessage();
        }*/
    }

I also put some code in the Global.asax file:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        SqlDependency.Start(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
    }
    protected void Application_End()
    {
        // Shut down SignalR Dependencies
        SqlDependency.Stop(ConfigurationManager.ConnectionStrings["TestData"].ConnectionString);
    }
}

The SQL server is located on the local machine. I am running the code through Visual Studio (IIS Express).

  • To enable a service broker in the database:

    ALTER DATABASE SET ENABLE_BROKER GO

  • In order to sign up for a request notification, we need to grant permission to an IIS account

    GRANT SUBSCRIBE QUERY NOTIFICATIONS TO "<serviceAccount>"
    

, , . . , , , . env. .

, , :

alter authorization on database::<dbName> to [sa];

.

0
1

: dependency.OnChange += new OnChangeEventHandler(OnChange); :

               SqlDependency dependency = new SqlDependency(command);

               dependency.OnChange += new OnChangeEventHandler(OnChange);

void OnChange(object sender, SqlNotificationEventArgs e)

0

Source: https://habr.com/ru/post/1761117/


All Articles