{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Home/issues/1202","labels_url":"https://api.github.com/repos/aspnet/Home/issues/1202/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Home/issues/1202/comments","events_url":"https://api.github.com/repos/aspnet/Home/issues/1202/events","html_url":"https://github.com/aspnet/Home/issues/1202","id":124156837,"number":1202,"title":"Feature Request - Need Something like HttpApplication (that's pooled and assocaited to a thread)","user":{"login":"matlus","id":10308689,"avatar_url":"https://avatars.githubusercontent.com/u/10308689?v=3","gravatar_id":"","url":"https://api.github.com/users/matlus","html_url":"https://github.com/matlus","followers_url":"https://api.github.com/users/matlus/followers","following_url":"https://api.github.com/users/matlus/following{/other_user}","gists_url":"https://api.github.com/users/matlus/gists{/gist_id}","starred_url":"https://api.github.com/users/matlus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matlus/subscriptions","organizations_url":"https://api.github.com/users/matlus/orgs","repos_url":"https://api.github.com/users/matlus/repos","events_url":"https://api.github.com/users/matlus/events{/privacy}","received_events_url":"https://api.github.com/users/matlus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":22,"created_at":"2015-12-29T03:08:59Z","updated_at":"2015-12-30T15:13:44Z","closed_at":null,"body":"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:\r\n1. HttpApplication was pool\r\n2. 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.\r\n\r\nThis 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\r\n```D\r\n((Global)HttpContext.Current.ApplicationIstance).MyProperty\r\n```\r\nGranted 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:\r\n1. Throughput and Scale\r\n2. Reduced pressure on the GC\r\n3. Improved performance\r\n\r\nIf 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\r\n\r\nI hope all of this makes sense?\r\n\r\nSome code examples below, just in case I've not been clear enough on how I used this in old ASP.NET\r\n\r\nThe 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.\r\n```D\r\npublic class MvcApplication : System.Web.HttpApplication\r\n{\r\n    private QInsightManager _qInsightManager;\r\n\r\n    internal QInsightManager QInsightManager\r\n    {\r\n        get { return _qInsightManager; }\r\n    }\r\n\r\n    public override void Init()\r\n    {\r\n        base.Init();\r\n        _qInsightManager = new QInsightManager();\r\n    }\r\n}\r\n```\r\nThe code below show how this pooled object can be consumed on an MVC ControllerBase class and made available to all descendants very simply.\r\n```D\r\ninternal QInsightManager QInsightManager\r\n{\r\n    get { return ((MvcApplication)HttpContext.ApplicationInstance).QInsightManager; }\r\n}\r\n``` \r\nThis implementation ensures that we grab the instance associated with the instance of Global that is handling the request."},"comment":{"url":"https://api.github.com/repos/aspnet/Home/issues/comments/168017286","html_url":"https://github.com/aspnet/Home/issues/1202#issuecomment-168017286","issue_url":"https://api.github.com/repos/aspnet/Home/issues/1202","id":168017286,"user":{"login":"benaadams","id":1142958,"avatar_url":"https://avatars.githubusercontent.com/u/1142958?v=3","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"created_at":"2015-12-30T15:13:44Z","updated_at":"2015-12-30T15:13:44Z","body":"Or if you want an object (so you can swap it for testing etc); then use `services.AddSingleton` and it will reuse the same object for everything."}}