I am not able to read the appSettings from the config file (MyService.exe.config) of my Windows Service. Please note that service is installed successfully.
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");
}
private void InitializeComponent()
{
try
{
AutoLog = true;
CanStop = true;
}
catch (Exception e)
{
// Log error
}
}
static void Main()
{
MyService myService = new MyService ();
Run(myService);
}
protected override void OnStart(string[] args)
{
// Code to do necessary things on start
}
}
The exception in the event viewer is System.Configuration.ConfigurationErrorsException
Which is the correct location to read the configuration of the Windows Service? ConfigurationManager.AppSettings returns null always.
The exception suggests that there is something wrong with your configuration file. Check it carefully. There should be more information in the exception or its inner exception which will give you a more precise indication of what’s wrong.