ASP.Net应用程序热身 - 揭露收藏应用程序、收藏、ASP、Net

由网友(一梦两三年)分享简介:我的MVC应用程序目前使用的的Global.asax ,的Application_Start 方法加载吨的数据,然后自曝它作为收藏品。例如:当前实例:// Global.asax中公共静态DataRepository库{获得;组; }保护无效的Application_Start(){//所有正常的东西...// p...

我的MVC应用程序目前使用的的Global.asax 的Application_Start 方法加载吨的数据,然后自曝它作为收藏品。例如:

当前实例:

  // Global.asax中
公共静态DataRepository库{获得;组; }
保护无效的Application_Start()
    {
        //所有正常的东西...

        // preLOAD这个仓库。
        DataRepository =新DataRepository();
    }

// HomeController.cs例
公众的ActionResult指数(){
    返回JSON(MyApplication.Repository.GetSomeCollection()
                JsonRequestBehavior.AllowGet);
}
 

我想要做什么:

我想使用 ASP.Net 4.0 + IIS 7.5的应用程序preLOAD 的功能,但需要的库暴露给应用程序的其余部分。是这样的:

在目标

  //伪code尝试

公共类应用preLOAD:IProcessHost preloadClient
{
    公共MyRepositoryClass库{获得;组; }

    公共无效preLOAD(字符串[]参数)
    {
        //库类的构造函数会谈DB和做其他垃圾。
        库=新MyRepositoryClass();
    }
}
 

问题

我怎么能暴露一个repository类,甚至一个简单的的IEnumerable< T> 收集使用 preLOAD()方法通过 IProcessHost preloadClient ? 解决方案 在网上下载的一个ASP.net 应用程序,用VS2010 调试出现未将对象引用设置到对象的实例的错误,求解决方法

如果你只是针对暴露的的IEnumerable< T> 尝试塞进 HttpRuntime.Cache IProcessHost preloadClient 的实施。然后,您可以选择公开从的Global.asax 应用程序类的集合。

是这样的:

 公共类应用程序preLOAD:IProcessHost preloadClient
{
    公共无效preLOAD(字符串[]参数)
    {
        VAR库=新MyRepositoryClass();
        HttpRuntime.Cache.Insert(
            集合名,
            repository.GetCollection(),
            Cache.NoAbsoluteExpiration,
            Cache.NoSlidingExpiration,
            CacheItemPriority.NotRemovable,
            空值);
    }
}

公共类MvcApplication:的HttpApplication
{
     公开的IEnumerable< CollectionItem>集合名
     {
         {返回HttpRuntime.Cache [集合名]为IEnumerable< CollectionItem取代; }
     }
}
 

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?

解决方案

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

阅读全文

相关推荐

最新文章