Reading configuration settings from a Windows Service app

D

Dhilip Kumar

Hi All,

I'm writing a Windows Service app using C#. I need to read some
configuration settings before the service starts up. These settings will be
used by the service in its operation. Question is, which is the best way to
store & retrieve the settings? I'm thinking of storing it in the registry
in HKLM\Software\ServiceName and access it using the Registry class in the
"public ServiceName()" method. I'm instructing the service installer to
use "Local Service" account since "Local System" has too many priveleges
which is risky. Would accessing the registry for start-up settings be fine,
or is there any risk associated with doing so? The other alternative is to
use a App.config file I guess. Which one is better? And what are the
possible risks in using the registry from a service?

Thanks,
Dhilip Kumar
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

If you only require read capabilities then use the app.config , it's local
to your app and .net gives you easy access to it in a transparent way.

You could also use the registry, there is nothing wrong doing so, but I
would keep there only those values that you need to change from your code
( app.config is ready only )

Cheers,
 
D

Dhilip Kumar

Hi Ignacio,

Thanks for your reply. Yes some of these values could be changed by the
end-user if needed. Hence the need to keep it in the registry.

I have one more question. While retrieving values from registry, I need to
make sure that some of the registry keys are present and have values. If
not I need to stop loading (starting the service). I tried to trigger the
OnStop method as follows but it doesn't help:

public OBMServerTry()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call

try
{
this.obmPollTime =
this.serverRegistryHKLM.GetValue("PollTime").ToString();
}
catch (Exception ex)
{
EventLog.WriteEntry("Exception occurred while trying to read polltime.
Error: " + ex.Message, EventLogEntryType.Error);
this.Onstop();
}
}

private void InitializeComponent()
{
this.performanceCounter1 = new
System.Diagnostics.PerformanceCounter("System","Processes", "");
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.performanceCounter1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = false;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// OBMServerTry
//
this.ServiceName = "OBMServerTry";
((System.ComponentModel.ISupportInitialize)(this.performanceCounter1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

//the following line is manually added
this.serverRegistryHKLM =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\CBM\\OBM");

}

What happens is that the try catch block is executed and an exception is
caught, but the service proceeds to start though I have triggered OnStop()!!
My guess at why the service starts is that the Windows Service project is
designed to receive commands from the SCM and hence it ignores the OnStop()
method triggered by the service code itself. Or am I missing something in
the code? I am just trying to find out how to stop loading the service if
any of the required registry parameters are missing and send an appropriate
response back to the SCM!

Thanks
Dhilip Kumar
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

For the changing values use the registry.

You can check it in the OnStart method if not found throw an exception IIRC
this will be logged in the event log under application.

Cheers,
 
D

Dhilip Kumar

Thanks Ignacio!

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

For the changing values use the registry.

You can check it in the OnStart method if not found throw an exception
IIRC this will be logged in the event log under application.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top