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?