简体   繁体   English

WCF在调用之间共享变量

[英]Wcf sharing variables between calls

I have a wcf web service which i want to share information between calls (diferent clients calls) . 我有一个wcf网络服务,我想在呼叫之间共享信息 (不同的客户呼叫)。

for example sharing a dictionary between client calls. 例如在客户呼叫之间共享字典。 the dictionary can be change through calls (add/remove item etc) but i cant efford it to be deleted or to be renew for each call (it should be like a static database) . 字典可以通过调用来更改(添加/删除项等),但是我不能为每次调用而删除或续订它(它应该像静态数据库一样)。

is there any way to implement this issue? 有什么办法可以解决这个问题?

allready tried a few suggestions but nothing realy seems to work. allready尝试了一些建议,但似乎没有任何实际效果。

You basically need a singleton defined outside of your service class - you would need to handle the very likely possibility that multiple calls will be accessing / modifying the info at the same time so will need to lock the data maybe with a ReaderWriteLock or ReaderWriterLockSlim . 基本上,您需要在服务类之外定义一个单例 -您将需要处理多个调用将同时访问/修改信息的可能性,因此可能需要使用ReaderWriteLockReaderWriterLockSlim锁定数据。

A nicer way of doing it, assuming you are using .net 4.0, would be to use MemoryCache , which contains built-in thread safety. 假设您使用的是.net 4.0,这样做的更好方法是使用MemoryCache ,它包含内置的线程安全性。 If you are pre .net 4.0, there is a cache object in the System.Web namespace link - it was a bit of a pain having to add reference to system.web if you wanted to use it when writing windows apps so the MemoryCache implementation in .net 4.0 was a welcome addition. 如果您是.net 4.0之前的版本,则System.Web命名空间链接中有一个缓存对象-如果要在编写Windows应用程序时使用system.web的引用,则不得不添加对system.web的引用有点麻烦.net 4.0中的版本是受欢迎的。

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

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