Solved

Local storage options for .NET Windows Service

Posted on 2014-10-20
12
251 Views
Last Modified: 2014-10-20
Hi,

I'm just starting to write a very simple windows service in .NET and I have a requirement to store data, from the last calculation to disk so that I can compare data even if the service or server is restarted.

I think SQL Server is over kill for this, so I thought about using LocalDB or IsolatedStorage as I have done in the past, but wondered if there is a better solution for such simple storage.

Thanks
0
Comment
Question by:wint100
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40392205
You can store your data to the LocalApplicationData directory that is accessed via System.Environment, e.g. -
System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)

Open in new window


In windows vista and higher this will return:
// For LocalSystem user
C:\Windows\ServiceProfiles\LocalService\AppData\Local

// For user named Peter
C:\Users\Peter\AppData\Local

Open in new window


-saige-
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40392420
Depends on what data you want to store. As well as local file storage, you've also got the registry
0
 
LVL 1

Author Comment

by:wint100
ID: 40392491
Thanks for the suggestions. I'll be storing
a list of objects and a timestamp, the list may have around 100 entries.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:Daniel Kline
ID: 40392526
I recommend you look at wrapping the file access in a mutex so that multiple concurrent service calls can't create a deadlock problem of failed request.

Where to place the data is based on a number of unspecified requirements, the most important of which is security.
0
 
LVL 1

Author Comment

by:wint100
ID: 40392533
Security isn't an issue, it isn't sensitive data at all.
0
 
LVL 5

Expert Comment

by:Daniel Kline
ID: 40392565
It's hard to say what would be best in your scenario, but it sounds very much like a state saving function that I created in the old days of mainframes and minis.  

In my solution, I saved the entire state of the application, including all internal variables to a disk storage immediately before running the update from the application.  If the server or connectivity failed during processing, the application when restarted would look for the recovery state and load the application from exactly the state before the update failure so the user could continue.  If the application ended normally, the recovery state record was deleted so that the application would initialize to a clean state when started.

Like your case, the information in the state was not sensitive and so a file storage was more than adequate.  The state recovery record was keyed with the user ID so that multiple concurrent users sessions could be managed.

The solution is simple but robust and file storage (which is persistent across sessions)  is all that was required.
0
 
LVL 1

Author Comment

by:wint100
ID: 40392573
How about isolated storage? I always found this worked well but didn't knownifnit was a good solution?
0
 
LVL 5

Expert Comment

by:Daniel Kline
ID: 40392581
"
 

--------------------------------------------------------------------------------
 
It seems like overkill and overly complex:

"When an application stores data in a file, the file name and storage location must be carefully chosen to minimize the possibility that the storage location will be known to another application and, therefore, will be vulnerable to corruption. Without a standard system in place to manage these problems, developing ad hoc techniques that minimize storage conflicts can be complex, and the results can be unreliable.

With isolated storage, data is always isolated by user and by assembly. Credentials such as the origin or the strong name of the assembly determine assembly identity. Data can also be isolated by application domain, using similar credentials."
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40392669
I would have to agree with Daniel.  Using isolated storage is either going to be overkill or overly complex.  As Daniel pointed out, your only concern right now should be what is accessing the data with regards to your service[s] and their respective threads.

-saige-
0
 
LVL 1

Author Comment

by:wint100
ID: 40392798
It is a very basic process running every 30mins, reads an email and parses out a value in the subject from lotus notes and saves that info to a file. That is it.

I mentioned isolatedstorage because inhale a class from another app I could simply copy over to handle all of the save and restore for me without much work at all. I do agree it can be complex but I wouldn't be starting from scratch.

I've not heard of 'mutex' before
0
 
LVL 5

Assisted Solution

by:Daniel Kline
Daniel Kline earned 250 total points
ID: 40392885
A Mutex, which probably is not needed in your scenario, is for "Mutual Exclusion".  In other words, only one at a time.  It a pattern for making sure that only one request or session can access a resource at a time ... protecting it from corruption by collisions and conflicting state data.  Put it on the pile for investigation later.  It doesn't look like the need is immediate.

Isolated storage could work in your situation, but it is more complex than necessary.  I have a philosophy of using the simplest solution possible, because you never know who will follow you and if they will have the skills to understand what you have created.  In the end, it's your choice.  That's one of the pleasures of being a developer.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40392894
Code reusability is a staple of good design.  As for a mutex, a mutex (Mutual Exclusion) is a thread synchronization mechanism used to ensure that:
No two concurrent processes are in their critical section at the same time; it is a basic requirement in concurrency control, to prevent race conditions.

Quoted source

-saige-
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question