简体   繁体   English

将ObjectCache与PerCall服务一起使用

[英]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.

相关问题 如何使用WCF PerCall服务保护资源 - How to protect resource using WCF PerCall Service 使用设置为ConcurrencyMode.Multiple和I​​nstanceContextMode.PerCall的WCF服务行为属性时是否可能出现并发问题? - Are concurrency issues possible when using the WCF Service Behavior attribute set to ConcurrencyMode.Multiple and InstanceContextMode.PerCall? 具有PerSession的WCF服务的行为类似于PerCall - WCF Service with PerSession acts like PerCall 在WCF PerCall服务上保留客户端的用户名 - Keeping the UserName of a client on WCF PerCall service WCF服务的ObjectCache或本地字段? - ObjectCache or local fields for my WCF service? 将WCF Windows服务作为InstanceContextMode.PerCall运行 - Running WCF Windows Service as InstanceContextMode.PerCall WCF是否使用ThreadPool为PerCall服务启用新实例? - Does WCF use the ThreadPool to bring up new instances for a PerCall service? 我可以在同一个服务中使用 PerCall 实例化和可重入并发吗? - Can I use PerCall instancing and Reentrant concurrency in the same service? WCF:对于无状态服务,perSession是否可以优于perCall - WCF: For a stateless service, can perSession be better over perCall 使用percall实例化可能的WCF异步双工回调? - WCF async duplex callbacks possible using percall instancing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM