简体   繁体   English

在 WCF 应用程序中缓存

[英]Caching in a WCF application

The below code throws an error of "Object reference not set to an instance of an object" When calling mycache.Get("products").下面的代码在调用 mycache.Get("products") 时抛出"Object reference not set to an instance of an object"的错误。 Im using a WCF application.我正在使用 WCF 应用程序。 Im not 100% im using caching correctly.我不是 100% 正确使用缓存。 Any advice?有什么建议吗?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Caching; 

namespace DataLayer {
    public class Products
    {
        public List<Product> Current()
        {
            Cache mycache = new Cache();
            if (mycache.Get("products")== null)
            {
                using (DataLayer.AccessDataContext db = new AccessDataContext())
                {
                    var products = from p in db.fldt_product_to_supp_parts
                                   where p.Current
                                   select new Product
                               {
                                   WholesaleCode = p.WholesaleCode,
                                   ProductCode = p.Product_Code
                               };
                    mycache["products"] = products.ToList();
                }

            }
            return mycache["products"] as List<Product>;
        }

    } }

EDIT : I'm using .net 3.5编辑:我正在使用 .net 3.5

I'm not sure what is wrong with your code, because I don't know off-hand how Cache is implemented, but a little searching uncovered the following Walkthrough from MSDN:我不确定您的代码有什么问题,因为我不知道Cache是如何实现的,但是稍微搜索一下就发现了 MSDN 中的以下演练:

http://msdn.microsoft.com/en-us/library/dd997362.aspx http://msdn.microsoft.com/en-us/library/dd997362.aspx

Caching Application Data in a WPF Application在 WPF 应用程序中缓存应用程序数据

And the following link gives an overview:以下链接提供了概述:

http://msdn.microsoft.com/en-us/library/dd997357.aspx http://msdn.microsoft.com/en-us/library/dd997357.aspx

In summary, it appears that for .NET v4 onwards, caching has been moved into System.Runtime.Caching from System.Web.Caching .总之,对于 .NET v4 及更高版本,缓存已从System.Web.Caching移至System.Runtime.Caching

From the look of the documentation you shouldn't be creating your own instance of the Cache class (it says the constructor is for framework use only).从文档的外观来看,您不应该创建自己的Cache class 实例(它说构造函数仅供框架使用)。 Try using Cache.Get instead?尝试改用Cache.Get吗?

EDIT (in response to comment)...编辑(回应评论)...

From the MSDN docs here :这里的 MSDN 文档:

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active.此 class 的一个实例是为每个应用程序域创建的,只要应用程序域保持活动状态,它就保持有效。 Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.有关此 class 实例的信息可通过 HttpContext object 的 Cache 属性或页面 object 的 Cache 属性获得。

So, it looks like Cache.Get is available when you're within a Page;因此,当您在 Page 中时,看起来Cache.Get是可用的; otherwise you can call HttpContext.Cache to get the active cache.否则你可以调用HttpContext.Cache来获取活动缓存。 Either way, there's a single Cache object for your entire application and you definitely shouldn't be creating one of your own.无论哪种方式,您的整个应用程序都有一个Cache object,您绝对不应该创建自己的缓存。

For non ASP.NET applications use caching from System.Runtime.Caching .对于非 ASP.NET 应用程序使用来自System.Runtime.Caching的缓存。

Your code throws System.NullReferenceException because internal cache CacheInternal of System.Web.Caching.Cache isn't initialized using internal void SetCacheInternal(CacheInternal cacheInternal) method.您的代码抛出System.NullReferenceException因为System.Web.Caching.Cache的内部缓存CacheInternal未使用internal void SetCacheInternal(CacheInternal cacheInternal)方法初始化。 It initializes by ASP.NET infrastructure in System.Web.HttpRuntime class.它由 System.Web.HttpRuntime class 中的System.Web.HttpRuntime基础设施初始化。

Unless I'm missing something, you're trying to use the ASP .NET cache within your WCF service.除非我遗漏了什么,否则您会尝试在 WCF 服务中使用 ASP .NET 缓存。 In order for this to work, you need to turn on ASP .NET compatibility using the AspNetCompatibilityRequirementsMode Enumeration .为了使其工作,您需要使用AspNetCompatibilityRequirementsMode Enumeration打开 ASP .NET 兼容性。 If you're self-hosting you'll have to roll your own.如果您是自托管,则必须自己动手。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CalculatorService : ICalculatorSession
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM