In old ASP.NET we had HttpApplication (Global) that was tagged to a thread which in turn was pooled by ASP.NET. So effectively, there was one instance of HttpApplication for each pooled thread and thus:
- HttpApplication was pool
- We had an instance of HttpApplication that was automatically associated to a thread and so no overhead was incurred synchronizing an instance of HttpApplication to a request thread (in our application) or accessing this instance from within an ASP.NET application such as a WebForm, Controller or Handler.
This was very useful, because if we need to pool any of our own classes we could simply create an instance of this class in Global (typically in the Init() method) and assign it to a custom property of Global and this property was directly accessible from a WebForm, Handler or (WebAPI) Controller like so
((Global)HttpContext.Current.ApplicationIstance).MyProperty
Granted all pooled "things" need to be stateless or need to zero out their state at the beginning of every request. The benefit of being able to something like this is:
- Throughput and Scale
- Reduced pressure on the GC
- Improved performance
If provided out of the box - makes it really easy for folks to get these benefits (caveat emptor), just like we were able to do riding on HttpApplication in old ASP.NET
I hope all of this makes sense?
Some code examples below, just in case I've not been clear enough on how I used this in old ASP.NET
The code below shows how one would initialize these pooled objects in Global. The QInsightManager is the property the makes the "pooled" object available to WebPages, Handlers, Controllers etc.
public class MvcApplication : System.Web.HttpApplication
{
private QInsightManager _qInsightManager;
internal QInsightManager QInsightManager
{
get { return _qInsightManager; }
}
public override void Init()
{
base.Init();
_qInsightManager = new QInsightManager();
}
}
The code below show how this pooled object can be consumed on an MVC ControllerBase class and made available to all descendants very simply.
internal QInsightManager QInsightManager
{
get { return ((MvcApplication)HttpContext.ApplicationInstance).QInsightManager; }
}
This implementation ensures that we grab the instance associated with the instance of Global that is handling the request.
In old ASP.NET we had HttpApplication (Global) that was tagged to a thread which in turn was pooled by ASP.NET. So effectively, there was one instance of HttpApplication for each pooled thread and thus:
This was very useful, because if we need to pool any of our own classes we could simply create an instance of this class in Global (typically in the Init() method) and assign it to a custom property of Global and this property was directly accessible from a WebForm, Handler or (WebAPI) Controller like so
Granted all pooled "things" need to be stateless or need to zero out their state at the beginning of every request. The benefit of being able to something like this is:
If provided out of the box - makes it really easy for folks to get these benefits (caveat emptor), just like we were able to do riding on HttpApplication in old ASP.NET
I hope all of this makes sense?
Some code examples below, just in case I've not been clear enough on how I used this in old ASP.NET
The code below shows how one would initialize these pooled objects in Global. The QInsightManager is the property the makes the "pooled" object available to WebPages, Handlers, Controllers etc.
The code below show how this pooled object can be consumed on an MVC ControllerBase class and made available to all descendants very simply.
internal QInsightManager QInsightManager { get { return ((MvcApplication)HttpContext.ApplicationInstance).QInsightManager; } }This implementation ensures that we grab the instance associated with the instance of Global that is handling the request.