无法连接到服务器

时间:2016-03-07 19:38:20

标签: c# rabbitmq

我有以下代码连接到本地计算机上的RabbitMQ但是当我将主机名从localhost更改为我的servername时,它失败并返回错误

var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "localhost";

我一改变HostName,就会返回错误

factory.HostName = "myserver";
  

异常:没有指定的端点可以访问

     

AMQP操作被中断:AMQP关闭原因,由...发起   库,代码= 0,text = \“流结束\”,classId = 0,methodId = 0,   cause = System.IO.EndOfStreamException:Peer错过了2次心跳   心跳超时设置为60秒

2 个答案:

答案 0 :(得分:1)

与使用sql一样,使用连接字符串连接更容易,而不是以这种方式连接。

示例C#

var factory = new ConnectionFactory
{
    Uri = ConfigurationManager.ConnectionStrings["RabbitMQ"].ConnectionString,
    RequestedHeartbeat = 15,
    //every N seconds the server will send a heartbeat.  If the connection does not receive a heartbeat within
    //N*2 then the connection is considered dead.
    //suggested from http://public.hudl.com/bits/archives/2013/11/11/c-rabbitmq-happy-servers/
    AutomaticRecoveryEnabled = true
};
return factory.CreateConnection();

web.config或app.config

  <connectionStrings>
    <add name="RabbitMQ" connectionString="amqp://{username}:{password}@{servername}/{vhost}" />
  </connectionStrings>

答案 1 :(得分:0)

在服务器上,看起来主机名配置不同。我的管理员查看了日志并查看了配置,并在服务器上为我提供了主机名。

var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/filestream";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "myserver";