ASP.NET persist Cache when AppDomain is refreshed - asp.net

When code changes are published to .NET websites the re-compilation process involves restarting the AppDomain, this in turn wipes the application cache.
Are there any events raised when this happens? Is there any way to manually serialise portions of my cached data and save it to disk, then subsequently re-initialise the cache when the application is loaded again?

you can use MS AppFabric caching, it's a free component for Windows Server and solves this by decoupling the caching from the ISS app domain.
see question and answers here: AppFabric vs System.Runtime.Caching
if you keep using ASP.NET Caching you are exposed at the issues you have described above and to my knowledge there is no solution.

Related

System.Web.Caching & System.Runtime.Caching against IIS App Pool restarts

In Asp.net Forms or MVC application, cache items which use System.Web.Caching get clear in an app pool restart.
But what happens if caching was achieved using System.Runtime.Caching ?
But what happens if caching was achieved using System.Runtime.Caching ?
If you are using the MemoryCache default implementation then exactly the same will happen because the items will be stored in memory of the AppDomain. If you want your cache items to survive application pool restarts you might need to use a distributed cache system such as Redis or Memcached. By using a distributed cache system all the nodes of your web cluster will have access to those cache items and might take advantage of them.

View cache of ASP.NET process

I want to view the cache keys and sizes of a running ASP.NET process. Is there any tools available to do this? Or any pointers on how to build a tool?
We have written a webservice methods which are only for internal use to view and refresh cache. It can delete a specific cache key as well. We have hosted it along with the mail website. You can come up with this app by using asp.net caching api's.
Also you can go through Monitor your ASP.NET Cache API Behaviour

ASP.NET warmup/initialize

I'm trying to eliminate (or at least minimize) startup/warmup times for my .NET applications. I'm not really sure on how to do this even though it's a common concern.
There's a ton of questions about slow startup of .NET applications. These are easily explained by pool recycles, worker process startup, dynamic compilation of .aspx files, JIT etc. In addition, there are more things that may need to be initialized within the application such as EntityFramework and application caches.
I've found alot of different solutions such as:
ASP.NET Precompilation
IIS 8 Application Initialization (and for IIS 7.5)
Auto-Start ASP.NET Applications
However, I'm not entirely satisfied with any of the solutions above. Furthermore I'm deploying my applications to Azure Websites (in most cases) so I have limited access to the IIS.
I know that there are some custom "warmup scripts" that uses various methods for sending requests to the application (e.g. wget/curl). My idea is to create a "Warmup.aspx" page in each of my ASP.NET applications. Then I have a warmup service that sends an HTTP GET to the Warmup.aspx of each site every ... 5 minutes. This service could be a WorkerRole in Azure or a Windows Service in an on-premise installation. Warmup.aspx will will then do the following:
Send an HTTP GET to each .aspx-file within the application (to
dynamically compile the page)
This could be avoided by precompiling the .aspx pages using aspnet_compiler.exe
Send a query to the database to
initialize EntityFramework
Initialize application caches etc
So, my final question is whether there are better alternatives than my "Warmup.aspx" script? And is it a good approach or do you recommend some other method? I would really like some official method that would handle the above criteria.
Any and all suggestions are welcome, thanks!
Did you try this IIS Auto-Start feature described here ?
https://www.simple-talk.com/blogs/2013/03/05/speeding-up-your-application-with-the-iis-auto-start-feature/
You could have two instances of the site. When you need to deploy a new version, and therefore suffer a startup cycle, remove one instance out of load balancer rotation, deploy and start it, set it in and do the same for instance 2. A rolling deployment.

WCF Cache vs. Page.Cache

I've got two different, but closely related ASP.Net web applications that use the same data on some pages. In both applications I am using the ObjectDataSource control, have EnableCaching="true", and use the same CacheKeyDependency value in both applications.
I would like to make it so that when a new record is inserted or deleted in one application, it clears the cache in both applications. I began by simply clearing cache by using Page.Cache, but soon realized that it does not clear the cache in the other application. Then I added a WCF service to each application; each service clears the cache object in the application it is hosted in. Except that it doesn't...
First, I discovered that System.Web.HttpContext is always null in WCF. Then I tried instantiating a System.Web.Routing.RequestContext object, but its HttpContext object is always null as well.
It all boils down to this: If I set a Page.Cache object, can a WCF service access that same cache object, if the service is hosted in the same application as the page?
Yes, you need to enable ASP.NET integration for the WCF service. This involves setting the aspNetCompatibilityEnabled attribute for the serviveHostingEnvironment element in config as well as adding the AspNetCompatibilityRequirementAttribute attribute to you service class to indicate that you support it.
More on this subject can be found here on MSDN.
The main challenge with cache in two applications is that the cache can be stored on seperate machines, or if they are on the same machine, in different application pools.
One way you can do this is to allow both applications to use the same cache. One solution for a distributed cache that runs out of process is Appfabric caching.

Determine if asp.net application support load balancing environment

I am new to asp.net, I have been tasked to upgrade the physical architecture to load balancing environment to support the application.
I done some reading the session state should be configure to Out-Process instead of In-Proc to support load balancing.
Is there any other issues I need to take note for asp.net web application to run in the load balanced environment?
if you are going to have your session out of proc, your session objects should be serializable. If you want a quick solution, sticky sessions are an option. You can read about it here

Resources