[英]Using ObjectCache with PerCall service
I currently have a WCF RESTful service with InstanceContextMode = InstanceContextMode .PerCall
. 我目前有
InstanceContextMode = InstanceContextMode .PerCall
的WCF RESTful服务。 I did this because the service methods interact with an Entity Framework model and I read that I should use PerCall
rather than Single
. 我这样做是因为服务方法与Entity Framework模型进行交互,并且我读到应该使用
PerCall
而不是Single
。 Now, I wanted to add a new service method that returns a large dataset. 现在,我想添加一个新的服务方法来返回一个大数据集。 My original thought was to spin a thread off on the service startup, and cache the dataset so when the service method is called, I wouldn't have to hit to database... I'd just return the cache.
我最初的想法是在服务启动时剥离线程,并缓存数据集,这样,在调用服务方法时,我就不必访问数据库了……我只是返回缓存。 The problem with that though is that I cannot do this because I'm using
PerCall
, so after the instance is destroyed, so is my cache. 但是,问题在于我无法执行此操作,因为我正在使用
PerCall
,因此在销毁实例之后,缓存也是如此。
My question is, what caching options do I have? 我的问题是,我有哪些缓存选项? Do I really need
PerCall
or can I make it a singleton, but just make sure that the EF context is new for every call? 我真的需要
PerCall
还是可以让它成为一个单例,但只需确保每个呼叫的EF上下文都是新的?
You are right about EF context and lifetime; 您对EF上下文和生存期是正确的; it should be as short-lived as possible.
它应该是短命的。 In a web(-service) scenario it usually means 'per request/call' (at the longest).
在网络(服务)场景中,通常表示“每个请求/调用”(最长)。
Off course, you could handle the lifetimes yourself, but this is a scenario in which an IoC container with DI really shines. 当然,您可以自己处理生命周期,但是在这种情况下,带有DI的IoC容器确实发光。 You just register your services and the EF context as 'per request' and the cache as a singleton, and the container will do the heavy lifting and injecting the correct dependencies for you.
您只需将服务和EF上下文注册为“每个请求”,将缓存注册为单例,容器将为您进行繁重的工作并为您注入正确的依赖项。 You can also do more fine grained lifetime scoping if that's desirable.
如果需要,还可以进行更细粒度的生命周期范围界定。
Some of the most common: 最常见的一些:
There are lots of good tutorials and guides for setting up the most popular IoC containers with WCF, like " How to Use Dependency Injection (Ninject) with WCF Services ". 关于使用WCF设置最受欢迎的IoC容器,有很多很好的教程和指南,例如“ 如何在WCF服务中使用依赖注入(Ninject) ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.