Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for storing settings for a .NET Windows Service: Service Property Settings, Serialization,

I am working on a .NET Windows Service where I am trying to store settings that will be used when the service is started and while it is running. I have searched through posts on SO and found that using the Settings in the properties of the project is great for use with console and winforms applications. However, Google and SO are silent when it pertains to storing these settings with a windows service.

Does anyone one know if it is proper to use these settings in a .NET service? If not, is Serialization my next best choice? Has anyone had practical uses for settings in a service and found that it is best to use a specific method?

like image 873
TheDevOpsGuru Avatar asked Aug 05 '10 13:08

TheDevOpsGuru


1 Answers

I normally use the registry to store information I need in my service i.e port etc.

string lsbkey = @"Software\mycompany\adas";

RegistryKey adaskey = Registry.LocalMachine.OpenSubKey(lsbkey, false);

try
{
    object regip = adaskey.GetValue("IP");
    object regport = adaskey.GetValue("PORT");
    localip = regip.ToString();
    localport = int.Parse(regport.ToString());
}
catch (NullReferenceException ne)
{
    localip = null;
    localport = 0;
    writelog(@"Aborting Service, IP or PORT doesn't exist in \local machine\software\mycompany\adas : "+ne.Message);
    status = 0;

}
like image 183
Adrian Thompson Avatar answered Oct 12 '22 23:10

Adrian Thompson