4

I have to implement a asp.net web api which acts as a subscriber to rabbitMQ. The windows service is going to publish message to the web api services. There will be more than one instance of web api running on production enviornment. I am not sure how to open up the subscriber channel on web api and keep it open untill the IIS restarts. There will be one publisher and several consumer.

Can anyone please guide with some sample code to start with?

Any help will hugely appreciated

user1213831
  • 309
  • 7
  • 22

1 Answers1

8

Generally RabbitMQ subscriptions don't work well with IIS hosted applications because you have no control over when the application is running. IIS will recycle, stop and start the app as it sees fit.

If you must do it, open the connection to RabbitMQ and start subscribing when the application starts, in Global.asax.cs for example, and make sure to dispose of everything properly when it closes.

You are far better off building a windows service for the subscription and either writing to a shared store that the IIS hosted web service can access, or alternatively self-hosting the API inside the windows service.

Mike Hadlow
  • 9,427
  • 4
  • 45
  • 37
  • Excellent reply. just to the point. Thanks very much for the reply. I need to open the subscription in web api on IIS because it acts as a push service for rest of the application. When you say during application_end dispose of objects you mean the subscriberbus.Dispose? – user1213831 Aug 31 '14 at 09:19
  • Also does ll subscribers with the same objecttype i.e. bus.subscribe("localhost") will get the message when publisher publish with the same object type? – user1213831 Aug 31 '14 at 09:22
  • Yes, EasyNetQ routes by message type, so subscribers of a type will get all published messages of that type. – Mike Hadlow Sep 01 '14 at 12:37
  • and subscriber.dispose() does all the magic when application ends? or I need to do anything else? – user1213831 Sep 01 '14 at 12:54
  • You need to bus.Dispose(); when your application exits. That will shut down all the consumers (subscribers) too. – Mike Hadlow Sep 02 '14 at 14:14
  • Excellent. Thanks very much for clearing all the doubts – user1213831 Sep 04 '14 at 10:54
  • you would have an example on github to have a base, because I'm using a Web Api core I don't know how to call the bus.Receive ("my.paymentsqueue", ... ? – Anderson Apr 22 '20 at 19:40