将 .NET 对象存储到内存并在退出后将其留在那里
我想知道是否有一种方法可以将对象加载到内存中并在程序存在后将其保留在那里。我想这样做是为了跨 WCF REST 服务共享配置信息,而不必在每次读取配置时都转到磁盘进行序列化/反序列化。我遇到的一个问题是不能保证某些东西总是运行来“保留”该对象。
我正在使用 C# 和 .NET 4.0。
这是一些模拟我将/希望做的事情的伪代码:
variable localmemoryobject
if memoryObject does not exist then
create the memory object
end if
copy the memory object into localmemoryobject
done
我正在研究内存映射文件来完成此任务,并且不确定这是否适合我的问题。有什么建议吗?
I was wondering if there was a way to load an object in memory and leave it there after the program exist. I want to do this to share configuration info across a WCF REST service without having to go to the disk for serialization/deserialization every time the config is read. An issue I am having is there is no guarantee that something is always running to "hold" the object.
I am using C# and .NET 4.0.
Here is some psuedo code that models what I will/hope to do:
variable localmemoryobject
if memoryObject does not exist then
create the memory object
end if
copy the memory object into localmemoryobject
done
I am looking into memory-mapped files to accomplish this and wasn't sure if this was appropriate for my issue. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想在内存中保存一些数据,那么至少需要有一个进程保持运行来存储它。您可能需要一个小进程来存储这些内存中的对象(例如 memcached)然后启动和关闭 WCF 服务,而不必担心将数据序列化到磁盘。
If you want to keep some data in memory, then there will need to be at least one process that remains running to store it. You may want to have a small process that you use for storing these in-memory objects (like memcached, for example) and then have your WCF services start-up and shutdown without worrying about serializing data to disk.
为您的配置添加一个 Windows 服务应用程序并使其保持活动状态怎么样?
How about adding a Windows service app for your configuration and keep that alive?
在 WCF 服务中使用企业库缓存块。
http://msdn.microsoft.com/en-us/library/ff649551.aspx
您的 WCF 服务需要读取一次配置,然后将其缓存在内存中以供将来调用,直到配置的缓存到期间隔已过,或者您重新启动服务。
Use Enterprise Libraries Caching Blocks within your WCF service.
http://msdn.microsoft.com/en-us/library/ff649551.aspx
Your WCF service will need to read the config once, and then it will be cached in memory for future calls, until either the configured cache-expiry interval is passed, or you restart the service.
也许 Windows 的内存映射文件 API 对您有用。
在这里查看更多内容:
http://msdn.microsoft.com/en-us/library/aa908742.aspx
Possibly Windows' Memory Mapped Files API can be of use to you.
See more here:
http://msdn.microsoft.com/en-us/library/aa908742.aspx