我们在Azure Service Bus客户端周围有以下包装器:
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;
}
我编写了一个集成测试,通过运行SendMessage
然后使用Service Bus Explorer 2.0查看邮件内容来检查这是否有效。我们这样做是为了确保我们可以使用该工具修改和重新发送消息来修改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
属性定义它。
如何解决此编码问题?我是否需要提供自己的serializer / stream或者是否有办法强制默认序列化程序正确编码?
答案 0 :(得分:8)
尝试以下代码。我测试了它,我可以使用我的工具(Service Bus Explorer: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; }
}
}