【发布时间】:2018-12-28 11:14:42
【问题描述】:
我正在使用以下命令在 Service Broker (SQL 2016) 上为我的队列创建对话:
BEGIN TRANSACTION
BEGIN DIALOG CONVERSATION @NotificationDialog
FROM SERVICE ChangeNotifications
TO SERVICE 'ChangeNotifications'
ON CONTRACT [http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]
WITH ENCRYPTION = OFF;
-- Send the message
--
SEND ON CONVERSATION @NotificationDialog
MESSAGE TYPE [http://schemas.microsoft.com/SQL/Notifications/QueryNotification] (@Message)
COMMIT TRANSACTION
我正在使用以下代码在 Windows 服务中接收消息:
using (SqlCommand cmd = new SqlCommand("WAITFOR ( RECEIVE * FROM dbo.NotificationsQueue);", cnn))
{
cmd.CommandTimeout = 0;
cnn.Open();
// Execute the command - we will wait here until a new entry appears in the Notification Queue
//
SqlDataReader reader = cmd.ExecuteReader();
// Get the message text from the reader
//
while (reader.Read())
{
// Get the message body text and convert into a legible format
//
messageText = reader.GetString(reader.GetOrdinal("message_body"));
messtype = reader.GetString(reader.GetOrdinal("message_type_name"));
convhandle = reader.GetGuid(reader.GetOrdinal("conversation_handle"));
}
reader.Close();
reader = null;
if (messtype == @"http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog" ||
messtype == @"http://schemas.microsoft.com/SQL/ServiceBroker/Error" ||
messtype == @"http://schemas.microsoft.com/SQL/Notifications/QueryNotification" )
{
var cmd2 = new SqlCommand("end conversation '" + convhandle.ToString() + "'", cnn);
cmd2.ExecuteNonQuery();
cmd2.Dispose();
}
}
当代码尝试执行结束对话时,我收到错误消息“找不到对话句柄。”。如果我在 sys.conversation_endpoints 中搜索句柄,它也不存在。我不会在任何地方主动结束谈话。 为什么没有记录?
【问题讨论】:
-
您是否正在运行存储过程?使用如下:cmd2.CommandType = System.Data.CommandType.StoredProcedure;
-
@jdweng,代码显然没有执行存储过程。
-
我在您发布的代码中看不到任何明显会导致此错误的内容,尽管我建议参数化
END CONVERSATION查询并像外部查询一样将其包围在using块中。跨度>
标签: c# sql-server-2016 service-broker