Jeffrey Kevin Pry
Jeffrey Kevin Pry

Reputation: 3296

Storing .NET object to memory and leaving it there after exit

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?

Upvotes: 0

Views: 1530

Answers (4)

Ran
Ran

Reputation: 6159

Possibly Windows' Memory Mapped Files API can be of use to you.

See more here: http://msdn.microsoft.com/en-us/library/aa908742.aspx

Upvotes: 1

BonyT
BonyT

Reputation: 10940

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.

Upvotes: 1

mtijn
mtijn

Reputation: 3678

How about adding a Windows service app for your configuration and keep that alive?

Upvotes: 1

dlev
dlev

Reputation: 48596

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.

Upvotes: 2

Related Questions