Groups | Blog | Home
all groups > asp.net caching > august 2006 >

asp.net caching : SQL Cache Dependency in application_start event.



masmith
8/7/2006 8:14:02 AM
I am trying to find the best way to get SQL Dependency Caching to work in the
Global.asax. In the Application_Start event I open a connection to the SQL
Server database and get data from several tables that will change very
infrequently. This data is then saved in Application variables. What I am
looking for is to be notified if the table has been changed so I can reload
the Application variables with the new information. I have SQL Cache
Dependency working for the database and the connection used to read the
tables does have the appropriate permissions to get notifications working. I
tried adding the following Global.asax:

//Note the following is called by Application_Start
Procedure_to_get_data…{
…
SqlDependency sqlDep = new SqlDependency(sqlCmd);
sqlDep.OnChange += new OnChangeEventHandler(sqlDep_IllnessList_OnChange);
…
}

private void sqlDep_IllnessList_OnChange(object caller,
SqlNotificationEventArgs e) {

Debug.WriteLine("sqlDep_OnChange");
const string LF = "\n\r";
if (e.Type == SqlNotificationType.Change) {
Debug.WriteLine(e.Info + LF + e.Source + LF + e.Type);
GetSQLConnStart();
GetIllnessList();
}

}//

The sqlDep_IllnessList_OnChange gets the initial Subscribe event but no
Change event when the data changes. I believe this is because after the
Global.asax is executed it is detroyed and the event I regestered no longer
exist.

Is there any way of making this work at the application level?



--
Alvin Bruney [MVP]
8/7/2006 10:05:31 PM
have sqlDep be a static, that will rule out collection. However, I dont
think that is the issue because the global class stays around as long as the
application does.

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------


[quoted text, click to view]

masmith
8/8/2006 8:01:02 AM
Thank you for your response!

After seeing your response that the Application instance stays around I took
another look at everything else that needed to be in place to make it work.
I found the stored procedure being used did not meet the requirements. After
removing the nocount and database name when referencing the table in the
select the SQL cache dependency now works.
--
Thanks,
Mark


[quoted text, click to view]
AddThis Social Bookmark Button