ASP.Net Application Warmup - Exposing Collections
Asked Answered
C

1

1

My MVC application currently uses the Global.asax, Application_Start method to load tons of data, and then exposes it as collections. For example:

Current Usage Example:

// Global.asax 
public static DataRepository Repository { get; set; }
protected void Application_Start()
    {
        // All the normal stuff...

        // Preload this repository.
        DataRepository = new DataRepository();
    }

// HomeController.cs Example
public ActionResult Index(){
    return Json(MyApplication.Repository.GetSomeCollection(), 
                JsonRequestBehavior.AllowGet);
}

What I'm trying to do:

I want to use the ASP.Net 4.0 + IIS 7.5 Application Preload functionality, but need to expose the repository to the rest of the application. Something like:

// pseudo code attempt at goal 

public class ApplicationPreload : IProcessHostPreloadClient
{
    public MyRepositoryClass Repository { get; set; }

    public void Preload(string[] parameters)
    {
        // repository class's constructor talks to DB and does other crap.
        Repository = new MyRepositoryClass();
    }
}

Question

How can I expose a repository class or even a simple IEnumerable<T> collection using the Preload() method implemented via IProcessHostPreloadClient?

Coastguardsman answered 17/7, 2012 at 0:43 Comment(0)
M
3

If you're just aiming to expose an IEnumerable<T> try stuffing it into HttpRuntime.Cache from the implementation of IProcessHostPreloadClient. You can then optionally expose the collection from the Global.asax application class.

Something like:

public class ApplicationPreload : IProcessHostPreloadClient
{
    public void Preload(string[] parameters)
    {
        var repository = new MyRepositoryClass();
        HttpRuntime.Cache.Insert(
            "CollectionName", 
            repository.GetCollection(), 
            Cache.NoAbsoluteExpiration, 
            Cache.NoSlidingExpiration, 
            CacheItemPriority.NotRemovable, 
            null);
    }
}

public class MvcApplication : HttpApplication
{
     public IEnumerable<CollectionItem> CollectionName
     {
         get { return HttpRuntime.Cache["CollectionName"] as IEnumerable<CollectionItem>; }
     }
}
Musing answered 17/7, 2012 at 4:15 Comment(5)
This is a good idea. The piece I was looking for was storing/fetching cache. Just to confirm, doing this still requires the IIS portion of the setup to ensure it is auto loaded and this piece fires before a user ever arrives. Am I understanding that correctly?Coastguardsman
That's correct. The IIS portion preloads the necessary data, but removes the dependency on the instance of the application so that when that starts up (the user arrives), the data you need is already in cache.Musing
Thought so. The issue I'm running into now is working with IIS Express with preloading. I have configured the external dev server's applicationHost.config properly, but I am not sure how to mimic this functionality in VS/IIS Express to debug the preload logic. Any ideas?Coastguardsman
I believe it requires IIS integrated pipeline mode, so you may have to attach to your local IIS process rather than using Casini.Musing
I am using IIS Express, not Casini -- nevermind. there is an applicationhost.config for IIS Express generally in User's Documents folder. I'll try that first.Coastguardsman

© 2022 - 2024 — McMap. All rights reserved.