我在使用MONO在C#中实现一个非常简单的RabbitMQ客户端时遇到了问题。我正在使用以下环境: 操作系统:Ubuntu 16.04 MonoDevelop:5.10 .net程序集:RabbitMQ.Client.dll版本3.6.5.0
我的电脑上运行了RabbitMQ服务器。如果我运行命令
sudo rabbitmqctl status
我获得以下结果
Status of node 'rabbit@federico-pc' ...
[{pid,9948},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.6.5"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.5"},
{webmachine,"webmachine","1.10.3"},
{mochiweb,"MochiMedia Web Server","2.13.1"},
{ssl,"Erlang/OTP SSL application","7.3"},
{public_key,"Public key infrastructure","1.1.1"},
{crypto,"CRYPTO","3.6.3"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.5"},
{asn1,"The Erlang ASN1 compiler version 4.0.2","4.0.2"},
{compiler,"ERTS CXC 138 10","6.0.3"},
{amqp_client,"RabbitMQ AMQP Client","3.6.5"},
{inets,"INETS CXC 138 49","6.2"},
{syntax_tools,"Syntax tools","1.7"},
{rabbit,"RabbitMQ","3.6.5"},
{mnesia,"MNESIA CXC 138 12","4.13.3"},
{os_mon,"CPO CXC 138 46","2.4"},
{rabbit_common,[],"3.6.5"},
{ranch,"Socket acceptor pool for TCP protocols.","1.2.1"},
{xmerl,"XML parser","1.3.10"},
{sasl,"SASL CXC 138 11","2.7"},
{stdlib,"ERTS CXC 138 10","2.8"},
{kernel,"ERTS CXC 138 10","4.2"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:2:2] [async-threads:64] [kernel-poll:true]\n"},
{memory,
[{total,55360120},
{connection_readers,0},
{connection_writers,0},
{connection_channels,0},
{connection_other,2712},
{queue_procs,2712},
{queue_slave_procs,0},
{plugins,389184},
{other_proc,18455960},
{mnesia,68360},
{mgmt_db,424248},
{msg_index,51504},
{other_ets,1445848},
{binary,98976},
{code,27797472},
{atom,1000601},
{other_system,5622543}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,1549926400},
{disk_free_limit,50000000},
{disk_free,231269703680},
{file_descriptors,
[{total_limit,924},{total_used,2},{sockets_limit,829},{sockets_used,0}]},
{processes,[{limit,1048576},{used,229}]},
{run_queue,0},
{uptime,5890},
{kernel,{net_ticktime,60}}]
这是非常简单的客户端的C#代码
using System;
using RabbitMQ.Client;
namespace provaRabbit
{
class MainClass
{
public static void Main (string[] args)
{
var factory = new ConnectionFactory ();
factory.HostName = "localhost";
factory.UserName = "test";
factory.Password = "test";
factory.VirtualHost = ConnectionFactory.DefaultVHost;
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
using (var connection = factory.CreateConnection ())
using (var channel = connection.CreateModel ())
{
channel.QueueDeclare (queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = System.Text.Encoding.UTF8.GetBytes (message);
channel.BasicPublish (exchange: "",
routingKey: "hello",
basicProperties: null,
body: body);
Console.WriteLine (" [x] Sent {0}", message);
}
Console.WriteLine (" Press [enter] to exit.");
Console.ReadLine ();
}
}
}
当然我创建了一个带有相关密码的“测试”用户,并且我给了他管理员权限。 当我尝试调试程序时,我获得了以下异常
RabbitMQ.Client.Exceptions.BrokerUnreachableException: None of the specified endpoints were reachable ---> System.MissingMethodException: Method 'IPAddress.MapToIPv6' not found.
at System.Linq.Enumerable+WhereSelectArrayIterator`2[TSource,TResult].MoveNext () [0x0004d] in <filename unknown>:0
at System.Linq.Buffer`1[TElement]..ctor (IEnumerable`1 source) [0x00087] in <filename unknown>:0
at System.Linq.Enumerable.ToArray[TSource] (IEnumerable`1 source) [0x00011] in <filename unknown>:0
at RabbitMQ.Client.TcpClientAdapter.BeginConnect (System.String host, Int32 port, System.AsyncCallback requestCallback, System.Object state) [0x00044] in <filename unknown>:0
at RabbitMQ.Client.Impl.SocketFrameHandler.Connect (ITcpClient socket, RabbitMQ.Client.AmqpTcpEndpoint endpoint, Int32 timeout) [0x0000f] in <filename unknown>:0
at RabbitMQ.Client.Impl.SocketFrameHandler..ctor (RabbitMQ.Client.AmqpTcpEndpoint endpoint, System.Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 writeTimeout) [0x0003f] in <filename unknown>:0
at RabbitMQ.Client.Framing.Impl.ProtocolBase.CreateFrameHandler (RabbitMQ.Client.AmqpTcpEndpoint endpoint, System.Func`2 socketFactory, Int32 connectionTimeout, Int32 readTimeout, Int32 writeTimeout) [0x00000] in <filename unknown>:0
at RabbitMQ.Client.ConnectionFactory.CreateFrameHandler (RabbitMQ.Client.AmqpTcpEndpoint endpoint) [0x00005] in <filename unknown>:0
at RabbitMQ.Client.ConnectionFactory.CreateConnection (IList`1 endpoints, System.String clientProvidedName) [0x0007e] in <filename unknown>:0
--- End of inner exception stack trace ---
at RabbitMQ.Client.ConnectionFactory.CreateConnection (IList`1 endpoints, System.String clientProvidedName) [0x0009b] in <filename unknown>:0
at RabbitMQ.Client.ConnectionFactory.CreateConnection (IList`1 hostnames, System.String clientProvidedName) [0x0001d] in <filename unknown>:0
at RabbitMQ.Client.ConnectionFactory.CreateConnection () [0x00013] in <filename unknown>:0
at provaRabbit.MainClass.Main (System.String[] args) [0x00029] in /home/federico/Scrivania/rabbitMQ/provaRabbit/provaRabbit/Program.cs:16
有没有人有任何建议?
此致
费德里科
答案 0 :(得分:0)
该错误通常意味着RabbitMQ未运行,或者位于无法访问的服务器上。
尝试一些事情,看看它是否正在运行,而不是“状态”。
运行sudo rabbitmqctl list_queues
如果两者都有效,那么rabbitmq肯定在运行。在这一点上,我想知道你是否有防火墙或安全设置阻止你连接到端口5672(RabbitMQ端口)上你自己的本地主机。
您可能还想在连接配置中添加“连接超时”。把它设置得高,比如2分钟。
(请参阅https://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v3.1.1/rabbitmq-dotnet-client-3.1.1-client-htmldoc/html/type-RabbitMQ.Client.ConnectionFactory.html - 以及connectionFactory.RequestedConnectionTimeout
设置)
你的机器可能只需要一段时间来建立连接,而C#代码认为它不可用。
答案 1 :(得分:0)
我找到了问题的解决方案。 诀窍是使用内置库:System.Messaging,Mono.Messaging,Mono.Messaging.RabbitMQ,RabbitMQ.Client。
一个非常简单的工作代码是
using System;
using System.Text;
using System.Messaging;
using Mono.Messaging;
using Mono.Messaging.RabbitMQ;
using RabbitMQ.Client;
namespace provaRabbitMQ
{
class MainClass
{
public static void Main (string[] args)
{
string message = "";
ConnectionFactory factory = new ConnectionFactory ();
factory.HostName = "localhost";
factory.Port = 5672;
factory.UserName = "guest";
factory.Password = "guest";
factory.RequestedHeartbeat = 60;
IConnection connection = factory.CreateConnection ();
IModel channel = connection.CreateModel ();
channel.QueueDeclare ("try", true);
message = "Hello World!";
byte[] body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "",
routingKey: "try",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
费德里科