MSMQ 和 ProtoBuf-Net 序列化 C#

发布于 2024-12-10 11:56:25 字数 955 浏览 1 评论 0原文

我有一个具有许多属性的类,其中之一是 byte[] 属性。我想使用 MSMQ 将此类发送到另一台服务器。但在发送之前,我想使用 ProtoBuf-Net 对其进行序列化。是否可以将 ProtoBuf-Net 序列化器附加到 MSMQ?

坦率地说,我不明白如何将课程发送到队列。我尝试使用下面的代码将类放入 MSMQ 队列,但出现异常“生成 XML 文档时出错。”

EntityBase 是我想发送的一个类。以前,我使用自己的基于文件系统的消息传递系统和 ProtoBuf-Net 来序列化类。

const string queueName = @".\private$\TestQueue";

private static void SendMessageToQueue(EntityBase entity)
{
    // check if queue exists, if not create it
    MessageQueue msMq = null;
    if (!MessageQueue.Exists(queueName))
    {
        msMq = MessageQueue.Create(queueName);
    }
    else
    {
        msMq = new MessageQueue(queueName);
    }
    try
    {

        msMq.Send(entity);
    }
    catch (MessageQueueException ee)
    {
        Console.Write(ee.ToString());
    }
    catch (Exception eee)
    {
        Console.Write(eee.ToString());
    }
    finally
    {
        msMq.Close();
    }
    Console.WriteLine("Message sent ......");
}

I have a class with many properties, one of them are byte[] property. I would like to send this class to another server using MSMQ. But before sending it I would like to serialize it using ProtoBuf-Net. Is it possible to attach ProtoBuf-Net serializer to MSMQ?

Frankly speaking I do not understand how to send class to queue. I have tried to use code below to put class to MSMQ queue but got exception "There was an error generating the XML document."

EntityBase is a class which I would like to send. Previously I have used my own File System based messaging system and ProtoBuf-Net to serialize class.

const string queueName = @".\private$\TestQueue";

private static void SendMessageToQueue(EntityBase entity)
{
    // check if queue exists, if not create it
    MessageQueue msMq = null;
    if (!MessageQueue.Exists(queueName))
    {
        msMq = MessageQueue.Create(queueName);
    }
    else
    {
        msMq = new MessageQueue(queueName);
    }
    try
    {

        msMq.Send(entity);
    }
    catch (MessageQueueException ee)
    {
        Console.Write(ee.ToString());
    }
    catch (Exception eee)
    {
        Console.Write(eee.ToString());
    }
    finally
    {
        msMq.Close();
    }
    Console.WriteLine("Message sent ......");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-12-17 11:56:25

我的建议是:发送 stringbyte[] 作为消息;您可以执行以下操作:

byte[] body;
using(var ms = new MemoryStream()) {
    Serializer.Serialize(ms, entity);
    body = ms.ToArray();
}

然后 Send(body),或者如果您想要 字符串,则 Send(Convert.ToBase64String(body))。并将其反转为另一端。

看起来还有一种机制可以将 MessageIMessageFormatter 一起传递,但我不确定它是否值得。读取示例:

Message msg = // receive
byte[] body = (byte[])msg.Body;
EntityBase entity;
using(var ms = new MemoryStream(body)) {
    entity = Serializer.Deserialize<EntityBase>(ms);
}

或(如果选择传递字符串):

string base64 = (string)msg.Body;
byte[] body = Convert.FromBase64String(base64);
// as before

My advice would be: send either a string or a byte[] as the message; you can do this as:

byte[] body;
using(var ms = new MemoryStream()) {
    Serializer.Serialize(ms, entity);
    body = ms.ToArray();
}

and then Send(body), or if you want a string, then Send(Convert.ToBase64String(body)). And reverse this as the other end.

There looks to also be a mechanism for passing a Message along with an IMessageFormatter, but I'm not sure it is worth it. Example of reading:

Message msg = // receive
byte[] body = (byte[])msg.Body;
EntityBase entity;
using(var ms = new MemoryStream(body)) {
    entity = Serializer.Deserialize<EntityBase>(ms);
}

or (if choosing to pass a string):

string base64 = (string)msg.Body;
byte[] body = Convert.FromBase64String(base64);
// as before
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文