我在目标队列中的消息在SQL Server Service Broker中不可读

时间:2019-01-21 13:46:01

标签: sql-server service-broker

我正在使用Service Broker连接系统中的其他域,如您所见,在这里我向目标服务发送了一条消息,如下所示:

gfortran  -Ofast -Wall -o "test1" "test1.f90"  
test1.f90:32:16:
      myarray(ra).list2;
            1
Error: Derived type ‘myarray’ cannot be used as a variable at (1)
test1.f90:29:17:
     sd= size(myarray);
             1
Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array
Compilation failed.

另一方面,我启用SP来获取如下消息:

_ctx.Database.ExecuteSqlCommand(@"Declare @ConversationHandle uniqueidentifier

Begin Transaction
Begin Dialog @ConversationHandle
 From Service SenderService
 To Service 'ReceiverService'
 On Contract SampleContract
 WITH Encryption=off;
SEND 
      ON CONVERSATION @ConversationHandle
      Message Type SenderMessageType
  ('<Reception>Add</Reception><OrganizationId>2</OrganizationId>') Commit");

如您所见,我将message_body插入ALTER PROCEDURE [dbo].[usp_ProcessTargetQueue] as BEGIN Declare @ConversationHandle as uniqueidentifier Declare @MessageBody as nvarchar(max) Declare @MessageType as sysname declare @MessageBodyXML as xml WHILE (1=1) BEGIN BEGIN TRANSACTION; WAITFOR ( RECEIVE top (1) @MessageType = message_type_name, @ConversationHandle = conversation_handle, @MessageBody = message_body FROM TargetQueue ), TIMEOUT 5000; IF (@@ROWCOUNT = 0) BEGIN ROLLBACK TRANSACTION; BREAK; END if @MessageType = 'SenderMessageType' BEGIN SEND ON CONVERSATION @ConversationHandle Message Type ReceiverMessageType ('Message is received') END Conversation @ConversationHandle set @MessageBodyXML= (select cast(@MessageBody as xml) ) insert into s (s1) select CONVERT(nvarchar(max), @MessageBodyXML) END COMMIT TRANSACTION; END END 表中,但是当我检查该表时,我看不到消息。 enter image description here

1 个答案:

答案 0 :(得分:2)

此代码将varchar值插入二进制message_body:

SEND 
      ON CONVERSATION @ConversationHandle
      Message Type SenderMessageType
  ('<Reception>Add</Reception><OrganizationId>2</OrganizationId>') Commit");

但是此代码将二进制值转换为nvarchar,因此该值被误解了:

insert into s (s1) select CONVERT(nvarchar(max), @MessageBodyXML)

解决此问题的一种方法是指定N前缀,以便xml字符串是Unicode文字:

SEND 
      ON CONVERSATION @ConversationHandle
      Message Type SenderMessageType
  (N'<Reception>Add</Reception><OrganizationId>2</OrganizationId>') Commit");