我在数据库中设置了多个 SQL Service Broker 队列,但之前没有遇到过这个问题。包含 XML 的消息正在转换为似乎主要是中文字符的内容。当我在将 XML 放入消息队列之前检查存储 XML 的变量时,我可以看到它是英文的并且是良好的 XML 格式。当我从队列中选择时,我会收到汉字。当我使用外部 C# 应用程序从队列中拉出时,这些字符也是我收到的。奇怪的是,如果我使用 DBArtisan 查看队列,我会看到格式良好的 XML。
下面的 XML 放入队列时会转换为下面的汉字
<?xml version="1.0" ?>
<Message>
<MachineName>The Super Duper Machine</MachineName>
<CollectionName>snl0013d</CollectionName>
<Action>Install</Action>
<EntryDateTime>Jul 9 2009 4:47PM</EntryDateTime>
</Message>
㼼浸敶獲潩㵮ㄢ〮•㸿†††䴼獥慳敧ാ
†††㰠慍档湩乥浡㹥桔畓数畄数慍档湩㱥䴯捡楨敮慎敭ാ
†††㰠潃汬捥楴湯慎敭猾汮〰㌱㱤䌯汯敬瑣潩乮浡㹥
††††䄼瑣潩㹮湉瑳污㱬䄯瑣潩㹮
††††䔼瑮祲慄整楔敭䨾汵†‹〲㤰†㨵〱䵐⼼湅牴䑹瑡呥浩㹥
†††⼼敍獳条㹥
下面是我用来将消息放入队列并选择它的 T-SQL。
declare @dialog_handle uniqueidentifier
,@msg varchar(max)
,@collection_name varchar(30)
set @collection_name = 'snl0013d'
set @msg =
N'<?xml version="1.0" ?>
<Message>
<MachineName>' + 'The Super Duper Machine' + '</MachineName>
<CollectionName>' + @collection_name + '</CollectionName>
<Action>' + 'Install' + '</Action>
<EntryDateTime>' + CAST(getdate() AS VARCHAR(100)) + '</EntryDateTime>
</Message>'
select @msg
set @dialog_handle = NEWID()
begin dialog conversation @dialog_handle
from service [SAPP_QUEUE_ResponseService]
to service 'SAPP_QUEUE_SubmitService'
on contract [SAPP_CONTRACT_Contract]
with encryption = off;
send on conversation @dialog_handle
message type [SAPP_MSG_MessageType]
(
@msg
);
end conversation @dialog_handle
with cleanup
select message_body
,conversation_handle
,CONVERT(nvarchar(max), message_body) as msg
from SAPP_QUEUE_SubmitQueue;