简体   繁体   中英

My asp.net mvc 4 application does not send/publish to rabbitmq

I have an application, an asp.net mvc 4 application. I want to use rabbitmq with easynetq. In my local computer, it works perfectly. But in the production environment which is windows server 2012, it does not send any message.

I could not understand, why it does not work. In log message, nothing unusual. IIS and rabbitmq is in the same machine.

    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"});
            };
      ...
     }        

Thanks in advance

Don't put it in a using statement, that will dispose the bus instance immediately when startup is complete, you want to keep the same instance during the life time of your application.

Instead save the instance somewhere (like a static variable), and dispose it in the application_end event, not session_end.

So more like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM