我在 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/