Solved

Local storage options for .NET Windows Service

Posted on 2014-10-20
12
229 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 32

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 42

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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 32

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 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now