MSMQ 和 ProtoBuf-Net 序列化 C#
我有一个具有许多属性的类,其中之一是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是:发送
string
或byte[]
作为消息;您可以执行以下操作:然后
Send(body)
,或者如果您想要字符串
,则Send(Convert.ToBase64String(body))
。并将其反转为另一端。看起来还有一种机制可以将
Message
与IMessageFormatter
一起传递,但我不确定它是否值得。读取示例:或(如果选择传递
字符串
):My advice would be: send either a
string
or abyte[]
as the message; you can do this as:and then
Send(body)
, or if you want astring
, thenSend(Convert.ToBase64String(body))
. And reverse this as the other end.There looks to also be a mechanism for passing a
Message
along with anIMessageFormatter
, but I'm not sure it is worth it. Example of reading:or (if choosing to pass a
string
):