Azure存储队列-CloudQueueMessage不同类型

标签 azure azure-storage azure-queues azure-storage-queues

我在 Azure 存储中有一个队列,我希望能够向队列添加不同的消息类型并将它们解析为特定类型。

例如。

public class Customer
{
   public Customer()
   {
   }

   public string Name { get; set;}
   public string Email { get; set;}
   public string Address { get; set;}
}

public class Employee
{
   public Employee()
   {
   }

   public string Id { get; set;}
   public string Name { get; set;}
   public string Email { get; set;}
}

我可以将它们序列化为 JSON 并将它们添加到队列中,但是如何在不知道消息类型的情况下将它们反序列化为其特定类型?

我如何知道下一条消息是客户还是员工?我可以在消息中添加某种属性:“这是客户”或“这是员工”...

因为我有一个辅助角色,它将在队列中查找消息并根据类型执行特定操作

get message from queue

If message = customer
  do this
else if message = employee
  do that
else 
  do nothing

最佳答案

我过去也这么做过。我已将对象的类型作为字符串记录到消息中,然后添加一些分隔符:# 然后添加 json 序列化字符串。

所以我的消息看起来像这样:

MyProject.Domain.Model.Product#{'Id':'42','ProductName':'SuperHumanEnchancer'}

在返回的路上,您读取分隔符之前的任何内容并将其视为类型名称。分隔符后的字符串将是您的 json 序列化字符串。

关于Azure存储队列-CloudQueueMessage不同类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24530948/

相关文章:

javascript - 将文件上传到 Windows Azure,仅提供文件的链接

Azure 队列检查消息数

java - JMS 消息监听器调用程序失败,原因 : Identifier contains invalid JMS identifier character '-' : 'x-request-id'

Azure 逻辑应用程序 - 从队列消息的原始输入中删除双引号

azure - 无法使用新门户在 Azure AD 中添加 Microsoft 帐户

azure - 使用 azure-cli 的 Azure Active Directory 访问 token

json - 将新文档添加到 Azure 搜索索引时出错

node.js - 使用 Alpine 部署 Azure Function - 未处理的 'error' 事件

azure - 页面 Blob 的 Azure 快照是否会创建副本?

c# - 监视 Azure Blob 存储中的容器是否发生更改的最佳方法是什么?