I was recently in a situation where I needed to run some background processes against some data and in-memory data structures that contained information that needed to be processed from time to time.
My application is an ASP.NET application hosted in a 3rd party environment where they have their trust level set to medium. They host the web applications in such a way as to not disrupt other applications due to my programming, so in a way, its a bit of like sandboxing. The host more than likely set these trust permissions in the machine.config file and didn't allow for the applications to override the settings, as most hosts would. Asking them to add a scheduled task would be an option, but highly unlikely due to security constraints.
One option is to create an .aspx page that had the functionality that I needed to get done and ping it every x seconds and have it do my work, but that requires an external source hitting my website, and it feels and smells dirty.
I ran accross Rob Howard's 10 Tips for Writing High-Performance Web Applications article on MSDN and found an entry on using a timer as a background thread. I decided to pursue this idea further by creating an HttpModule that loads up assemblies at runtime into AppDomains and executes a common method (much like a plug in pattern) to do the work. I ran into a really big issue using AppDomains, so I came up with an alternate way of loading the assemblies using reflection.
In the end I came up with a solution that would run in a high/full trusted environment AND a medium trusted environment. The HttpModule will basically "dumb down" to the lowest setting specified and load the assemblies in that fashion. This allows for better isolation in higher trusted environments, but maintain the base functionality of running tasks in the background in the case of medium trust.
The concept is pretty simple: load assemblies from a given directory (or current executing directory), load types specified in the configuraiton file for the given assemblies, make sure they are compliant to the proper plug in interface, and finaly execute methods on them. It's not very difficult to get something like this up and running, so I'll be posting a binary and possibly the source code as soon as I get it cleaned up, documented, tested and a bit more pretty, so it may take me a few days or week or so.
Sunday, January 14, 2007
Running background processes in a medium trust environment
Posted by Marko Rangel at 9:49 PM
Labels: .net, technology
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment