0

我有一个应用程序,一个 asp.net mvc 4 应用程序。我想将rabbitmq 与easynetq 一起使用。在我的本地计算机上,它运行良好。但是在windows server 2012的生产环境中,它不会发送任何消息。

我不明白,为什么它不起作用。在日志消息中,没有什么异常。IIS 和 rabbitmq 在同一台机器上。

    protected void Application_Start()
    {
    ...
          using (var bus = RabbitHutch.CreateBus("host=localhost"))
          {
            bus.Publish(new SystemLog() { Code = "startapplication", Message = "nomessage" });
          }        
    ...
    }

    void Session_End(object sender, EventArgs e)
    {
      ...
            using (var bus = RabbitHutch.CreateBus("host=localhost"))
            {
                bus.Publish(new SystemLog() { Code = "sessionends", Message = "somenumber"});
            };
      ...
     }        

提前致谢

4

1 回答 1

2

不要将它放在 using 语句中,它将在启动完成后立即处理总线实例,您希望在应用程序的生命周期内保持相同的实例。

而是将实例保存在某处(如静态变量),并将其放置在 application_end 事件中,而不是 session_end 中。

所以更像这样:

protected void Application_Start()
{
  _bus = RabbitHutch.CreateBus("host=localhost"))
  _bus.Publish(new SystemLog() { Code = "startapplication", Message = "nomessage" });
}

void Session_End(object sender, EventArgs e)
{
  _bus.Publish(new SystemLog() { Code = "sessionends", Message = "somenumber"});
}

protected void Application_End()
{
  if(_bus!=null)
    _bus.Dispose();
}
于 2015-12-17T09:54:13.733 回答