Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some databases that work with single ASP.NET MVC project and all of the databases are the same and i used real time alarm with signalr but when i want to start sqldependencyin Application_Start method needed connection string, i set company name to cookie when user want to login and pass cookie to Application_Start to know which sqldependencyin which database started . but Application_Start run for first user,then for another user in another database i cant start sqldependency, and cookie is null in this method. please help


What I have tried:

protected void Application_Start(object sender, EventArgs e)
   {
       SqlConnectionString = ConfigurationManager.ConnectionStrings[HttpContext.Current.Request.Cookies["zone"].Value.ToString()].ConnectionString;

       if (!String.IsNullOrEmpty(SqlConnectionString))
           {
               if (SqlConnectionString.ToLower().StartsWith("metadata") && SqlConnectionString != null)
               {
                   System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efbeulder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(SqlConnectionString);

                   SqlConnectionString = efbeulder.ProviderConnectionString;

                   SqlDependency.Start(SqlConnectionString);
               }

           }

   }
Posted
Updated 12-May-20 0:25am

1 solution

As the name suggests, the Application_Start event fires when the application starts. It doesn't run again until the application is restarted.

It will also potentially run before any user has logged in, which would mean you wouldn't have a cookie value.

According to the documentation[^], you can safely call SqlDependency.Start multiple times for the same connection, and only one listener will start for each connection string. So the simplest solution is to move your code to the Application_BeginRequest event instead:
C#
protected void Application_BeginRequest(object sender, EventArgs e)
{
    // Make sure we have a cookie value:
    var cookie = HttpContext.Current.Request.Cookies["zone"];
    if (cookie == null || string.IsNullOrEmpty(cookie.Value)) return;
    
    // Make sure the connection string exists:
    var connectionStringSetting = WebConfigurationManager.ConnectionStrings[cookie.Value];
    if (connectionStringSetting == null) return;
    
    // Make sure the connection string has a value:
    string sqlConnectionString = connectionStringSetting.ConnectionString;
    if (string.IsNullOrEmpty(sqlConnectionString)) return;
    
    // Turn an Entity Framework connection string into a SQL connection string:
    if (sqlConnectionString.StartsWith("metadata", StringComparison.OrdinalIgnoreCase))
    {
        var efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(sqlConnectionString);
        sqlConnectionString = efBuilder.ProviderConnectionString;
    }
    
    // Start the SQL dependency:
    SqlDependency.Start(sqlConnectionString);
}
 
Share this answer
 
Comments
mohamad_ali 12-May-20 3:16am    
Thnx for your answer, and when i want to stop sqldepndency?
can i do it in begin request method?
first stop and then start
Richard Deeming 12-May-20 6:05am    
Why would you want to stop it and then immediately start it again? What would that achieve?
mohamad_ali 12-May-20 6:21am    
because if sqldependency is started before start again i want to stop it
Richard Deeming 12-May-20 6:23am    
According to the documentation, if you call Start multiple times with the same connection string, it will only start the listener for that connection once. Subsequent calls will return false to indicate that the listener for that connection has already been started.
Richard Deeming 12-May-20 6:27am    
If it's a new question, better to post it as a new question[^] so that others get the chance to see it and help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900