我已将 RabbitMQ .NET 客户端 DLL 移植到 MonoTouch 项目(iOS 项目),我可以向 RabbitMQ 服务器发送消息,但我无法接收任何消息并导致异常,现在我发布这些代码:
bool _DoneFlag=false;
QueueingBasicConsumer _receiveMessageConsumer;
string _QueueName;
IConnection _RMQConnection;
string RMQServerIP;
string LocalIP;
ConnectionFactory factory;
IModel channel;
BasicDeliverEventArgs messageInEnvelope;
IModel channel;
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "172.16.1.24:";
factory.Port=55672;
// 接受消息的队列名称是本地的IP地址
_QueueName = "172.16.1.1";
_RMQConnection = factory.CreateConnection();
channel = _RMQConnection.CreateModel();
channel.QueueDeclare(_QueueName, true, false, true, null);
_receiveMessageConsumer = new QueueingBasicConsumer(channel);
channel.BasicConsume(_QueueName, true, _receiveMessageConsumer);
while (_DoneFlag==false) {
object result = null;
_receiveMessageConsumer.Queue.Dequeue(400, out result);
BasicDeliverEventArgs messageInEnvelope = (BasicDeliverEventArgs)result;
if (messageInEnvelope == null) {
Console.WriteLine("messageInEnvelope == null)");
continue;
}
byte[] bytes = messageInEnvelope.Body;
string str=System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine("接受到的消息:" + str);
//RMQMessage msg = JsonConvert.DeserializeObject<RMQMessage>(System.Text.Encoding.UTF8.GetString(bytes));
}
---->:这些代码在此代码行中停止:“_RMQConnection = factory.CreateConnection();” 并导致“System.ObjectDisposedException”,这些是信息:
System.ObjectDisposedException:对象在被释放后被使用。在 System.Threading.WaitHandle.CheckDisposed () [0x00016] 在 /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/WaitHandle.cs:425 在 System.Threading.EventWaitHandle.Set () [0x00000 ] 在 /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/EventWaitHandle.cs:129 (wrapper remoting-invoke-with-check) System.Threading.EventWaitHandle:Set () at System.Net .Sockets.Socket+SocketAsyncResult.set_IsCompleted (Boolean value) [0x00024] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net.Sockets/Socket_2_1.cs:354 at System.Net.Sockets.Socket +SocketAsyncResult.Complete () [0x00037] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net.Sockets/Socket_2_1.cs:243 在 System.Net.Sockets.Socket+SocketAsyncResult.Complete (System .
* *注意:此代码在 .NET Visual Studio 2010 项目(控制台和 GUI)中正确运行。