我在SignalR中使用了SQLdependency来向用户显示警报。。代码如下:
public IEnumerable<AlertInfo> GetData(long UserId)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["yafnet"].ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT [AlertID],[AlertNote],[AlertDetails],[AlertDate],[Location]
FROM [dbo].[Alerts] where [UserID]=" + UserId + " AND [IsViewed]=0", connection))
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["yafnet"].ConnectionString);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["yafnet"].ConnectionString);
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
using (var reader = command.ExecuteReader())
return reader.Cast<IDataRecord>()
.Select(x => new AlertInfo()
{
AlertID = x.GetInt64(0),
AlertNote = x.GetString(1),
AlertDetails = x.GetString(2),
AlertDate = x.GetDateTime(3),
Location = x.GetString(4)
}).ToList();
}
}
}
它在本地主机上运行良好。但在上传到Azure服务器后,此方法会抛出以下错误:
消息":"发生错误。","ExceptionMessage":"不支持语句'RECEIVE MSG'在此版本的SQL Server中。","ExceptionType":"System.Data.SqlClient.SqlException","StackTrace":"\\r\n服务器堆栈跟踪:\r\n位于System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,操作`1 wrapCloseInAction)
可能是什么问题?
实际上,您的SQL Server数据库必须具有is_broker_enabled = 1
。您需要检查它是否已启用。
要对此进行验证,请使用命令SELECT name, is_broker_enabled FROM sys.databases
。如果您的数据库显示结果为"1",则可以;如果结果为"0",则必须使用此命令ALTER DATABASE yourdb SET ENABLE_BROKER
启用它。
但坏消息是,Azure SQL数据库显示它已启用,但不再支持is_broker_enabled
。
为此,您需要将SQL Server的完整实例安装到Azure VM。