Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a long lived object in within the IIS/ASP.Net?

I am currently using SignalR in and ASP.Net MVC 4 appliation. I am consuming messages from RabbitMQ, and in essence need to broadcast via SignalR whenever a new message hits the queue. Problem is under normal usage there is not a place where a long lived object can live within IIS. I am getting about 1000 messages a second, so the standard approach of pushing the message into IIS by making a request from an outside queue monitoring service/app would pretty much kill my IIS.

I have a general idea of creating a singleton instance on a background thread. Not sure whats the best way to do this in iis, would want the singleton to automatically be recreated if the application dies.

like image 898
apalmer Avatar asked Dec 15 '12 03:12

apalmer


1 Answers

Are you thinking you would have something in a background thread that would check for messages every so often?

I have used quartz.net to create scheduled jobs in the past. They are fairly simple to set up. You can basically just say, execute this job every x interval starting at y time. With whatever solution you go with you will probably need to add in error handling. I think your quartz job would keep trying to execute every x interval even if it threw an exception, but you will need to make sure that you clear out whatever caused the exception in the first place. Otherwise every time it runs it will fail. I.E. like if there is something wrong with your message such that it will error every time you try and broadcast it.

Watch out for IdleTimeout of your application. If IIS puts your web app to sleep your singleton in your background worker/quartz job goes to sleep too. If you set IdleTimeout to 0 your application will never sleep.

If you init your job/worker in Global.asax.cs Application_Start() your job will always start when your web app does.

When you first deploy your app or update it or restart it you will need to make sure your app is running. Not sure if there is a setting for this in IIS. But normally your application doesn't start up until a request is made to it. Good luck. Let me know if you find a solution to this.

Same deal if you app crashes for some other reason. You need something to re-start your web app.

Hope that helps!

like image 180
Ben Tidman Avatar answered Sep 24 '22 06:09

Ben Tidman