7

我们有以下围绕 Azure 服务总线客户端的包装器:

public virtual void SendMessage(object bodyObject)
{
    var brokeredMessage = 
        _messageBodySerializer.SerializeMessageBody(bodyObject);

    _queueClient.Send(brokeredMessage);
}

包括SerializeMessageBody

public BrokeredMessage SerializeMessageBody(object bodyObject)
{
    var brokeredMessage = new BrokeredMessage(bodyObject);

    brokeredMessage.Properties[MESSAGE_TYPE_PROPERTY_KEY] = 
                    bodyObject.GetType().AssemblyQualifiedName;

    return brokeredMessage;
}

我编写了一个集成测试,通过运行然后使用Service Bus Explorer 2.0SendMessage查看消息内容来检查它是否有效。我们这样做是为了确保我们可以使用该工具来修复和重新发送消息以修改 XML。我发送的消息如下所示:

[DataContract]
public class TestServiceBusMessage
{
    [DataMember]
    public Guid ExternalIdentifier { get; set; }

    [DataMember]
    public int Identifier { get; set; }

    [DataMember]
    public string Name { get; set; }
}

问题是; 当在工具上查看消息正文时,它看起来不像 XML,而是如下所示:

@TestServiceBusMessageWhttp://schemas.datacontract.org/2004/07/IntegrationTests.Common.Azure.ServiceBus   i)http://www.w3.org/2001/XMLSchema-instance@ExternalIdentifier?$13d81b90-d932-437f-a11c-9106412b6c4a@
Identifier?&    @Name?Test Message   

我认为这是一个编码问题;BrokeredMessage但是,在将正文传递给构造函数时,我看不到设置编码的方法;或通过DataContract属性定义它。

我将如何解决这个编码问题?我需要提供自己的序列化器/还是有办法强制默认序列化器正确编码?

4

1 回答 1

7

试试下面的代码。我对其进行了测试,我可以使用我的工具(服务总线资源管理器:http ://code.msdn.microsoft.com/windowsazure/Service-Bus-Explorer-f2abca5a#content )查看 XML 格式的有效负载。基本上,您需要在 BrokeredMessage 对象的构造函数中显式指定 DataContractSerializer。希望这可以帮助。乔保罗

#region MyRegion
using System;
using System.Runtime.Serialization;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging; 
#endregion

namespace StackOverflow
{
    static class Program
    {
        #region Private Constants
        private const string QueueName = "stackoverflow";
        private const string MessageType = "MessageType";
        private const string AssemblyName = "AssemblyName";
        private const string ConnectionString = "<your-service-bus-namespace-connectionstring>"; 
        #endregion

        #region Static Methods
        static void Main()
        {
            SendMessage();
        }

        static async void SendMessage()
        {
            try
            {
                // Create NamespaceManager object
                var namespaceManager = NamespaceManager.CreateFromConnectionString(ConnectionString);
                Console.WriteLine("NamespaceManager successfully created.");

                // Create test queue
                if (!namespaceManager.QueueExists(QueueName))
                {
                    namespaceManager.CreateQueue(new QueueDescription(QueueName)
                    {
                        RequiresDuplicateDetection = false,
                        RequiresSession = false,
                        LockDuration = TimeSpan.FromSeconds(60)
                    });
                    Console.WriteLine("Queue successfully created.");
                }

                // Create MessagingFactory object
                var messagingFactory = MessagingFactory.CreateFromConnectionString(ConnectionString);
                Console.WriteLine("MessagingFactory successfully created.");

                // Create MessageSender object
                var messageSender = await messagingFactory.CreateMessageSenderAsync(QueueName);
                Console.WriteLine("MessageSender successfully created.");

                // Create message payload
                var testServiceBusMessage = new TestServiceBusMessage
                {
                    ExternalIdentifier = Guid.NewGuid(),
                    Identifier = 1,
                    Name = "Babo"
                };

                // Create BrokeredMessage object
                using (var brokeredMessage = new BrokeredMessage(testServiceBusMessage,
                                                                 new DataContractSerializer(typeof(TestServiceBusMessage)))
                {
                    Properties = {{MessageType, typeof(TestServiceBusMessage).FullName},
                                  {AssemblyName, typeof(TestServiceBusMessage).AssemblyQualifiedName}}
                })
                {
                    //Send message
                    messageSender.SendAsync(brokeredMessage).Wait();
                }
                Console.WriteLine("Message successfully sent.");
                Console.WriteLine("Press [Enter] to exit");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press [Enter] to exit");
                Console.ReadLine();
            }
        } 
        #endregion
    }

    [DataContract]
    public class TestServiceBusMessage
    {
        [DataMember]
        public Guid ExternalIdentifier { get; set; }

        [DataMember]
        public int Identifier { get; set; }

        [DataMember]
        public string Name { get; set; }
    }
}
于 2013-07-16T13:37:19.437 回答