在MONO中实现RabbitMQ客户端时出现的问题

在MONO中实现RabbitMQ客户端时出现的问题,mono,rabbitmq,Mono,Rabbitmq,我在MONO下用C#实现一个非常简单的RabbitMQ客户机时遇到了一个问题。我正在使用以下环境: 操作系统:Ubuntu 16.04 单发:5.10 .net程序集:RabbitMQ.Client.dll版本3.6.5.0 我的电脑上运行着RabbitMQ服务器。如果我运行命令 sudo rabbitmqctl status 我得到以下结果 Status of node 'rabbit@federico-pc' ... [{pid,9948}, {running_applications,

我在MONO下用C#实现一个非常简单的RabbitMQ客户机时遇到了一个问题。我正在使用以下环境: 操作系统:Ubuntu 16.04 单发: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
RabbitMQ.Client.Exceptions.brokerRunReachableException:指定的终结点均不可访问-->System.MissingMethodException:找不到方法“IPAddress.MapToIPv6”。
位于System.Linq.Enumerable+WhereSelectArrayInterator`2[TSource,TResult].MoveNext()[0x0004d]中:0
在0中的System.Linq.Buffer`1[TElement]…ctor(IEnumerable`1源)[0x00087]处
位于System.Linq.Enumerable.ToArray[TSource](IEnumerable`1源)[0x00011]中的:0
在RabbitMQ.Client.TcpClientAdapter.BeginConnect(System.String主机、Int32端口、System.AsyncCallback请求回调、System.Object状态)[0x00044]中:0
在RabbitMQ.Client.Impl.SocketFrameHandler.Connect(ITcpClient套接字,RabbitMQ.Client.AmqpTcpEndpoint端点,Int32超时)[0x0000f]中:0
在RabbitMQ.Client.Impl.SocketFrameHandler..ctor(RabbitMQ.Client.AmqpTcpEndpoint端点,System.Func`2 socketFactory,Int32 connectionTimeout,Int32 readTimeout,Int32 writeTimeout)[0x0003f]中:0
在RabbitMQ.Client.Framing.Impl.ProtocolBase.CreateFrameHandler(RabbitMQ.Client.AmqpTcpEndpoint端点,System.Func`2 socketFactory,Int32 connectionTimeout,Int32 readTimeout,Int32 writeTimeout)[0x00000]in:0
位于:0中的RabbitMQ.Client.ConnectionFactory.CreateFrameHandler(RabbitMQ.Client.AmqpTcpEndpoint端点)[0x00005]
在RabbitMQ.Client.ConnectionFactory.CreateConnection(IList`1端点,System.String ClientProviderName)[0x0007e]中,位于:0
---内部异常堆栈跟踪的结束---
在RabbitMQ.Client.ConnectionFactory.CreateConnection(IList`1端点,System.String clientProvidedName)[0x0009b]中,位于:0
在RabbitMQ.Client.ConnectionFactory.CreateConnection(IList`1主机名,System.String clientProvidedName)[0x0001d]中:0
位于:0中的RabbitMQ.Client.ConnectionFactory.CreateConnection()[0x00013]
在/home/federico/Scrivania/rabbitMQ/provaRabbit/provaRabbit/provaRabbit/provaRabbit/provaRabbit/Program.cs:16中的provaRabbit.MainClass.Main(System.String[]args)[0x00029]处
有人有什么建议吗

问候


Federico

该错误通常意味着RabbitMQ未运行,或者位于根本无法访问的服务器上

除了“状态”之外,尝试几件事情来查看它是否正在运行

运行
sudo rabbitmqctl列表\u队列

和/或去

如果这两种方法都有效,rabbitmq肯定正在运行。在这一点上,我想知道您是否有防火墙或安全设置阻止您连接到端口5672(RabbitMQ端口)上自己的本地主机

您可能还希望在连接配置中添加“连接超时”。把它调高,比如2分钟

(请参阅-和
connectionFactory.RequestedConnectionTimeout
设置)


可能您的计算机只是花了一段时间建立连接,而C代码认为它因此不可用。

该错误通常意味着RabbitMQ没有运行,或者位于根本无法访问的服务器上

除了“状态”之外,尝试几件事情来查看它是否正在运行

运行
sudo rabbitmqctl列表\u队列

和/或去

如果这两种方法都有效,rabbitmq肯定正在运行。在这一点上,我想知道您是否有防火墙或安全设置阻止您连接到端口5672(RabbitMQ端口)上自己的本地主机

您可能还希望在连接配置中添加“连接超时”。把它调高,比如2分钟

(请参阅-和
connectionFactory.RequestedConnectionTimeout
设置)


有可能您的机器只是花了一段时间建立连接,C代码因此认为它不可用。

我找到了问题的解决方案。 诀窍是使用内置库: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();

        }
    }
}

费德里科我找到了这个问题的解决办法。 诀窍是使用内置库: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();

        }
    }
}

费德里科

首先,感谢您的回复。我确信RabbitMQ服务器正在运行,因为我可以访问端口15672上的web管理器,并且系统响应shell命令。我试图将connectionFactory.RequestedConnectionTimeout属性更改为120000,但得到了相同的结果。首先,感谢您的回复。我确信RabbitMQ服务器正在运行,因为我可以访问端口15672上的web管理器,并且系统响应shell命令。我尝试将connectionFactory.RequestedConnectionTimeout属性更改为120000,但得到了相同的结果。