How can I use System.Web.Caching.Cache in a Console application?

后端 未结 6 656
死守一世寂寞
死守一世寂寞 2021-02-01 03:47

Context: .Net 3.5, C#
I\'d like to have caching mechanism in my Console application.
Instead of re-inventing the wheel, I\'d like to use System.Web.Caching.Cache

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 04:06

    I ended on this page wondering the same thing. Here's what I'm doing (which I don't like but seems to work just fine):

    HttpContext context = HttpContext.Current;
    if (context == null)
    {
        HttpRequest request = new HttpRequest(string.Empty, "http://tempuri.org", string.Empty);
        HttpResponse response = new HttpResponse(new StreamWriter(new MemoryStream()));
        context = new HttpContext(request, response);
        HttpContext.Current = context;
    }
    this.cache = context.Cache;
    

提交回复
热议问题