无法反序列化从Azure Service Bus队列返回的消息

时间:2019-05-07 18:24:05

标签: json azure model-view-controller queue servicebus

我所在的团队正在编写专有的学习管理系统,当用户注册在线课程时,该系统需要发送电子邮件通知。

我编写了一个测试控制台应用程序,以测试将电子邮件排队到Azure Service Bus队列,然后接收和处理消息。

我创建了一个电子邮件对象

[DataContract(Name = "Email", Namespace = "Email")]
[Serializable]
public class Email
{
    [DataMember]
    public string From { get; set; }

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

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

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

我将消息发送到队列,如下所示:v

Email email = new Email
{
    From = "email@email.com",
    To = "email@email.com",
    Subject = "Testing Azure Service Bus Queue",
    Body = "The test was a success!"
};

BrokeredMessage message = new BrokeredMessage(email);

Console.WriteLine("Sending message to Azure");
SendToAzure sendClient = new SendToAzure();
try
{
     sendClient.Send(message);
}

public class SendToAzure
{
    public string ServiceBusConnectString = [Endpoint];
    public string QueueName = [Queue];

    public async Task Send(BrokeredMessage message)
    {
        QueueClient AzureClient = QueueClient.CreateFromConnectionString(ServiceBusConnectString, QueueName);
        await AzureClient.SendAsync(message);
    }
}

消息正在正确地放入队列。我可以进入我的Azure Service Bus队列,看看有一条消息排队。

下面是我的代码,用于接收消息并将其反序列化为Email对象:

public class ReadFromAzure
    {
        public string ServiceBusConnectString = [Endpoint];
        public string QueueName = [Queue];

    public void Receive()
    {
        QueueClient AzureClient = QueueClient.CreateFromConnectionString(ServiceBusConnectString, QueueName);
        while (true)
        {
            BrokeredMessage message = AzureClient.Receive();
            if (message != null)
            {
                var type = message.GetType();
                Email email = message.GetBody<Email>();
            }
        }
    }
}

当我调用GetBody函数并尝试将其反序列化为Email对象时,出现以下错误:

  

正在从名称空间“ Email”中期待元素“ Email”。遇到名称为“ BrokeredMessage”,名称空间为“ http://schemas.microsoft.com/netservices/2011/06/servicebus”的“ Element”。

有人知道导致此错误的原因吗?

0 个答案:

没有答案