我希望通过C#将消息发送到服务代理队列。我正在发送这样的命令:
string send = String.Format("BEGIN TRANSACTION; BEGIN DIALOG {0} FROM SERVICE [/AVAILH/xml/InitService] TO SERVICE N'/AVAILH/xml/CreateFile' ON CONTRACT [/AVAILH/xml/Contract] WITH ENCRYPTION = OFF; SEND ON CONVERSATION {0} MESSAGE TYPE [/AVAILH/xml/CreateFile] ({1}); COMMIT TRANSACTION; GO", runID, xmlMessage);
ThreadPool.QueueUserWorkItem(delegate
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using(SqlCommand command = new SqlCommand(send, conn))
{
conn.Open();
command.ExecuteNonQuery();
}
}
});
然而,当再次调用它时,我得到一个错误。
类型' System.Data.SqlClient.SqlException'未处理的异常发生在System.Data.dll
中其他信息:' 2'附近的语法不正确。
关键字'('(....)
附近的语法不正确
我只想弄清楚一种简单的方法将消息发送到队列,这将会重复发生。
非常感谢任何帮助或指导!
答案 0 :(得分:2)
var sendCmd = @"
BEGIN TRANSACTION;
DECLARE @cid UNIQUEIDENTIFIER;
DECLARE @xml XML = @message;
/* Begin dialog the conversation id into this variable. */
BEGIN DIALOG @cid FROM SERVICE [/AVAILH/xml/InitService]
TO SERVICE N'/AVAILH/xml/CreateFile'
ON CONTRACT [/AVAILH/xml/Contract]
WITH ENCRYPTION = OFF;
SEND ON CONVERSATION @cid
MESSAGE TYPE [/AVAILH/xml/CreateFile] (@xml);
/* if you don't do this you will have converations open forever unless
you clean them up later */
END CONVERSATION @cid;
COMMIT TRANSACTION;
";
using(var conn = new SqlConnection(connectionString))
using(var command = new SqlCommand(sendCmd, conn))
{
command.Parameters.AddWithValue("@message", xmlMessage);
conn.Open();
command.ExecuteNonQuery();
}
如果您希望使用同一转化而不是END CONVERSATION @cid
发送更多邮件,那么SELECT @cid
并使用var cid = (Guid)command.ExecuteScalar();
我还建议移动END CONVERSATION
收到结束或错误消息时,向内部激活的阅读器发送,并在阅读器中使用END CONVERSATION
确认初始消息,以获取上述消息。