【问题标题】:How to send a brokermessage that is a list of other types如何发送作为其他类型列表的代理消息
【发布时间】:2013-11-22 06:00:53
【问题描述】:

我正在尝试将代理消息发送到服务总线,并且我希望该消息是多种类型的列表。我尝试过使用接口和对象,它工作正常,直到我向列表中添加多个类型。我读过几篇关于做类似事情的帖子和在线文章,它们似乎都特定于进行手动 xml 序列化或使用 WCF。在这种情况下,序列化是自动发生的。

我的代码是这样的:

   Queue<Object> x = new Queue<Object>();
                    x.Enqueue(new VRequest());
                    x.Enqueue(new PRequest());
                    ServiceBus.TrackerClient.SendAsync(new BrokeredMessage(x) { ContentType = "BulkRequest" });

然后是我的代理消息处理程序(发生序列化错误的地方):

  var bulk = message.GetBody<Queue<Object>>();

关于如何发送具有多种类型的单个代理消息的任何想法?

【问题讨论】:

    标签: c# asp.net-mvc serialization


    【解决方案1】:

    对于任何感兴趣的人,您可以使用二进制格式化程序和内存流来完成此操作。它非常灵活,因为您正在处理二进制数据......您甚至可以使用接口等。一旦您拥有内存流,您将需要转换为字节数组,以便您可以通过网络发送它。然后你可以在另一端反序列化它。还要确保将对象标记为可序列化。

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            Queue<IYodas> q = new Queue<IYodas>();
            q.Enqueue(new Yoda());
            q.Enqueue(new Yoda2());
    
            formatter.Serialize(stream, q);
            Byte[] package = stream.ToArray();
    
           // Send broker message using package as the object to send
           ....
    
            // Then on the other end (you will need a byte array to object function)
            Queue<IYodas> result = (Queue<IYodas>)ByteArrayToObject(package);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-27
    • 2011-02-21
    • 2019-12-11
    • 2016-08-09
    • 1970-01-01
    • 2011-11-14
    • 2013-03-26
    相关资源
    最近更新 更多