ASP.Net Application Warmup - Exposing Collections

会有一股神秘感。 提交于 2019-12-24 15:26:34

问题


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?


回答1:


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>; }
     }
}


来源:https://stackoverflow.com/questions/11514606/asp-net-application-warmup-exposing-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!