msmq 使用 binaryformatter
msmq using binaryformatter
我有一个写入队列的组件(代码如下):
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
transaction.Begin();
using (var queue = new MessageQueue(@fullQueue, QueueAccessMode.Send))
{
BinaryMessageFormatter formatter = new BinaryMessageFormatter();
// XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(Testing) });
var testing = new Testing {myBody = string.Format("Hello {0}",Environment.UserName), myMessageText = "Header"};
var message = new Message
{
Body = testing,
Label = Environment.MachineName,
UseDeadLetterQueue = true,
Recoverable = true,
Formatter = formatter
};
queue.Send(message, MessageQueueTransactionType.Single);
}
transaction.Commit();
}
现在,当我 运行 时,上面的 'Testing' 只是一个带有 2 个属性的 Serializble 对象。如果我查看 Queue 上的文本正文,它看起来没问题。
现在我有一个单独的组件可以从队列中读取:
BinaryMessageFormatter formatter = new BinaryMessageFormatter();
// XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(Testing) });
MessageQueue msgQ = new MessageQueue(fullQueue, QueueAccessMode.Receive);
try
{
using (TransactionScope transaction = new TransactionScope())
{
Message incoming = new Message { Formatter = formatter, AcknowledgeType = AcknowledgeTypes.FullReceive };
incoming = msgQ.Receive(new TimeSpan(0, 0, 3),MessageQueueTransactionType.Single);
// var ttt = incoming.Body;
MemoryStream mem = (MemoryStream) incoming.BodyStream;
mem.Seek(0, SeekOrigin.Begin);
IFormatter ifm = new BinaryFormatter();
Testing tt = (Testing)ifm.Deserialize(mem);
transaction.Complete();
}
}
现在我正在执行 ifm.Deserialize,它出错
无法找到程序集'MSMQWrite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'其中 MSMQWrite 是我的写入组件中的方法名称。
当写入队列时,我想我可能必须首先显式序列化对象,所以在我写入的组件上我稍微更改了我的代码:
var testing = new Testing {myBody = string.Format("Hello {0}",Environment.UserName), myMessageText = "Header"};
MemoryStream mem = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mem,testing);
var message = new Message
{ BodyStream = mem,
//Body = testing,
Label = Environment.MachineName,
UseDeadLetterQueue = true,
Recoverable = true,
Formatter = formatter
};
queue.Send(message, MessageQueueTransactionType.Single);
如您所见,这次我直接设置了 BodyStream,但在尝试从队列中读取时仍然收到相同的错误。
您需要从第二个组件引用包含测试 class 的程序集。我猜那个程序集是 MSMQWrite。如果您已经引用它,请确保将其复制到第二个组件 运行 所在的文件夹,以便在运行时可以找到它。
另一种选择是将正在序列化的实际 class 移动到不同的组件(例如 MSMQCommon),并在您的组件编写和一次读取中引用它。或者只有一个组件。
我有一个写入队列的组件(代码如下):
using (MessageQueueTransaction transaction = new MessageQueueTransaction())
{
transaction.Begin();
using (var queue = new MessageQueue(@fullQueue, QueueAccessMode.Send))
{
BinaryMessageFormatter formatter = new BinaryMessageFormatter();
// XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(Testing) });
var testing = new Testing {myBody = string.Format("Hello {0}",Environment.UserName), myMessageText = "Header"};
var message = new Message
{
Body = testing,
Label = Environment.MachineName,
UseDeadLetterQueue = true,
Recoverable = true,
Formatter = formatter
};
queue.Send(message, MessageQueueTransactionType.Single);
}
transaction.Commit();
}
现在,当我 运行 时,上面的 'Testing' 只是一个带有 2 个属性的 Serializble 对象。如果我查看 Queue 上的文本正文,它看起来没问题。
现在我有一个单独的组件可以从队列中读取:
BinaryMessageFormatter formatter = new BinaryMessageFormatter();
// XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(Testing) });
MessageQueue msgQ = new MessageQueue(fullQueue, QueueAccessMode.Receive);
try
{
using (TransactionScope transaction = new TransactionScope())
{
Message incoming = new Message { Formatter = formatter, AcknowledgeType = AcknowledgeTypes.FullReceive };
incoming = msgQ.Receive(new TimeSpan(0, 0, 3),MessageQueueTransactionType.Single);
// var ttt = incoming.Body;
MemoryStream mem = (MemoryStream) incoming.BodyStream;
mem.Seek(0, SeekOrigin.Begin);
IFormatter ifm = new BinaryFormatter();
Testing tt = (Testing)ifm.Deserialize(mem);
transaction.Complete();
}
}
现在我正在执行 ifm.Deserialize,它出错
无法找到程序集'MSMQWrite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'其中 MSMQWrite 是我的写入组件中的方法名称。
当写入队列时,我想我可能必须首先显式序列化对象,所以在我写入的组件上我稍微更改了我的代码:
var testing = new Testing {myBody = string.Format("Hello {0}",Environment.UserName), myMessageText = "Header"};
MemoryStream mem = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mem,testing);
var message = new Message
{ BodyStream = mem,
//Body = testing,
Label = Environment.MachineName,
UseDeadLetterQueue = true,
Recoverable = true,
Formatter = formatter
};
queue.Send(message, MessageQueueTransactionType.Single);
如您所见,这次我直接设置了 BodyStream,但在尝试从队列中读取时仍然收到相同的错误。
您需要从第二个组件引用包含测试 class 的程序集。我猜那个程序集是 MSMQWrite。如果您已经引用它,请确保将其复制到第二个组件 运行 所在的文件夹,以便在运行时可以找到它。
另一种选择是将正在序列化的实际 class 移动到不同的组件(例如 MSMQCommon),并在您的组件编写和一次读取中引用它。或者只有一个组件。