我第一次使用Sql Server Service Broker,试图在特定表中发生更改时订阅通知。我发现了以下情况例外,当我打电话command.ExecuteReader()
:使用SqlDependency无法接收更改通知
System.InvalidOperationException:当使用的SqlDependency没有提供期权价值,SqlDependency.Start()之前,必须执行命令叫加入的SqlDependency实例。
我创建了一个测试,以重现的场景(留出一些对简洁的相关方法),具体如下:
private string _queueName = "EventsToPublishChangeMessages";
private bool _notificationReceived;
[Test]
public void WhyDoesExceptionIndcateSqlDependencyStartHasNotBeenCalledPriorToCommandExecuteReader()
{
Console.WriteLine($"canRequestNotifications: {CanRequestNotifications()}"); // returns true
var connectionString = GetConnectionString();
var started = SqlDependency.Start(connectionString, _queueName); // exception below seems to suggest that I haven't started the SqlDependency. Am I doing something wrong on this line?
Console.WriteLine($"Started:{started}"); // returns true
var connection = new SqlConnection(connectionString);
var command = new SqlCommand("SELECT Id, EventType, [Data], Created FROM dbo.EventsToPublish", connection);
var sqlDependency = new SqlDependency(command);
sqlDependency.OnChange += OnChange;
connection.Open();
// The following line causes the exception:
// System.InvalidOperationException : When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.
// But you can see that I *did* call SqlDependency.Start() above.
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Process(reader);
}
}
TryToTriggerANotification(connectionString);
Thread.Sleep(5000); // ie. wait a few seconds to ensure notification-handling background thread has had a chance to complete.
Assert.That(_notificationReceived, Is.True);
}
private void TryToTriggerANotification(string connectionString)
{
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(
"INSERT INTO dbo.EventsToPublish(Id, EventType, [Data], Created) VALUES(newid(), 'StackOverflowQuestionTest', '', GETDATE())",
connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
}
private void OnChange(object sender, SqlNotificationEventArgs e)
{
_notificationReceived = true;
}
private bool CanRequestNotifications()
{
try
{
var sqlClientPermission = new SqlClientPermission(PermissionState.Unrestricted);
sqlClientPermission.Demand();
return true;
}
catch
{
return false;
}
}
我发出下面的SQL查询来尝试更好地了解发生了什么:
select * from sys.service_queues -- can see my EventsToPublishChangeMessages queue
select is_broker_enabled from sys.databases where database_id=db_id() -- returns 1
select * from sys.dm_qn_subscriptions -- returns nothing
select * from sys.transmission_queue -- returns nothing
有没有人有任何想法,我可能做错了什么?
是否https://stackoverflow.com/questions/19155819/error-when-using-sqldependency-without-providing-an-options-value help?或者https://stackoverflow.com/questions/27411100/sqldependency-startconnectionstring-return-every-time-false? – mjwills
@mjwills第二个链接的答案有助于解决问题,即。使用带选项参数的SqlDependency构造函数。如果您想发布回复而不是评论,我很乐意将其标记为答案。谢谢! –