I have been following this post for developing a client for query notification. http://www.youdidwhatwithtsql.com/started-query-notifications-sql-server-2008-r2/1676/ I have tried this code on both visual studio and mono on my PC and these seem to fire the onDependencyChange event fine. However when I move it over to the raspberry pi with mono-complete installed it does not seem to fire. I cannot debug as the pi is in another location and I am using SSH to remote into it.
static void Main(string[] args)
{
Console.WriteLine ("-----------------APPLICATION STARTED------------------");
var connection = new SqlConnection(connectionString);
SqlDependency.Start(connectionString);
RefreshDataWithSqlDependency();
Console.WriteLine ("Why is it here?");
//blocks thread so you can read message
Console.ReadLine();
SqlDependency.Stop(connectionString);
}
static void RefreshDataWithSqlDependency()
{
//remove existing dependency if necessary
if (dependency != null)
{
dependency.OnChange -= onDependencyChange;
dependency = null;
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("SELECT ipAddress FROM dbo.dbDevices", connection);
//Create a dependency and associate it with command
dependency = new SqlDependency(command, null, 1);
//Subscribe to the SqlDependency event.
dependency.OnChange += new OnChangeEventHandler(onDependencyChange);
//Start dependency listener
SqlDependency.Start(connectionString);
//execute command and refresh data
RefreshData(command);
}
}
private static void onDependencyChange(Object o, SqlNotificationEventArgs args)
{
Console.WriteLine("ondep gets hit");
if ((args.Source.ToString() == "Data") || (args.Source.ToString() == "Timeout"))
{
Console.WriteLine("Refreshing data due to {0}", args.Source);
RefreshDataWithSqlDependency();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Data not refreshed due to unexpected SqlNotificationEventArgs: Source={0}, Info={1}, Type={2}", args.Source, args.Info, args.Type.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
}
}
private static void RefreshData(SqlCommand command)
{
using (var reader = command.ExecuteReader())
{
Console.Clear();
while (reader.Read())
{
Console.WriteLine("ip = {0}", reader[0]);
}
}
}
I have now put an extra Console.WriteLine just under RefreshDataWithSqlDependency method and when I use Run > Run With > Microsoft .NET or Mono 4.0.2 it seems to jump straight out of RefreshDataWithSqlDependency, however when I run with debugger it acts as it should. It will fire the event.