2016-08-19 45 views
1

我在MONO下的C#中實現一個非常簡單的RabbitMQ客戶端時遇到了問題。我使用以下環境: 操作系統:Ubuntu的16.04 的MonoDevelop:5.10 .NET程序集:RabbitMQ.Client.dll版本3.6.5.0在MONO中實現RabbitMQ客戶端的問題

我有一個RabbitMQ的服務器在我的電腦上運行。如果我運行命令

sudo rabbitmqctl status 

我得到以下結果

Status of node '[email protected]' ... 
[{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}}] 

這是

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(); 


     } 
    } 
} 

當然,我創建了一個「測試」很簡單的客戶端的C#代碼用戶使用相關的密碼,我給了他管理員權限。 當我嘗試調試程序我得到以下異常

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

這個錯誤通常意味着的RabbitMQ沒有運行,或者是不能在所有訪問的服務器上。

嘗試一些事情,看看它是否正在運行,而不是「狀態」。

運行sudo rabbitmqctl list_queues

和/或去http://localhost:15672

如果這兩個工作,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#代碼認爲這是因爲,無法使用。

+0

首先,感謝您的答覆。我確信RabbitMQ服務器正在運行,因爲我可以在端口15672上訪問Web管理器,並且系統響應shell命令。我試圖將connectionFactory.RequestedConnectionTimeout屬性更改爲120000,但我獲得了相同的結果。 –

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(); 

     } 
    } 
} 

費德里科