0

I have a Window Service , that is running on my machine . I have separate console application in the same solution that is performing some functionality. In order to access the functions of the console application , I have add the *.exe file of the console application as a reference to the Window Service project.

public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
    // TODO: Insert monitoring activities here.  
    EventLog.WriteEntry("Monitoring the System", EventLogEntryType.Information);
    string[] arr = new string[] { };
    ConsoleApplication.Program.Main(null);
}

The Console application works perfectly file if I directly run through Visual Studio. However , If I call the Main() method from the Window Service I am getting a null pointer exception.

public static void Main(string[] args)
{
    try
    {
       //CODE BREAKS HERE ON READING FROM CONFIG FILE
        string genesis_sel= ConfigurationSettings.AppSettings["genesis_sel"].ToString();
    }
    catch (Exception ex)
    {
       //Exception code
    }
}

Below is a snapshot of the Console application Project running in Visual Studio.

enter image description here

I am not sure what's causing the code to break while accessing the main method from a Window service.

Any suggestions?

Update: Added snapshot of the config file. I don't believe it's a rad access issue from the configuration file as it is reading correctly when I run the console application alone. There is an initialization issue when I access it through the window Service.

enter image description here

UPDATE Replaced the main method with a single event log , but still getting the same exception. enter image description here

Ananth Kumble
  • 152
  • 2
  • 15

1 Answers1

1

When you call the method it is running as part of your service, so it uses Environment and settings from your service. That means that you should have correct data in AppSettings of your service project. To bring more clarity: In this case function Main is part of your service and not a separate application root.

Otherwise you can run your console as separate process, but in that case you are loosing part of Control functions.

I would suggest, to separate common logic in a separate project/dll and call functions from it, it will be more clean and not so confusing

ASpirin
  • 3,601
  • 1
  • 23
  • 32
  • I replaced the config code with an event log, but its throwing the same error when running through the Window Service – Ananth Kumble Sep 13 '17 at 07:09
  • " The null exception". I think its got to with initialization . Somehow when calling through the Window Service, the values are not initialized and when running as a console application they are. Your thoughts ? – Ananth Kumble Sep 13 '17 at 07:13
  • I would suggest, to separate common logic in a separate project/dll and call functions from it, it will be more clean and not so confusing – ASpirin Sep 13 '17 at 07:14
  • The Window service and the console application are separate projects in the same solution file . All the logic is in the console project that is invoked from the service – Ananth Kumble Sep 13 '17 at 07:16
  • 1
    I mean create one `Logic` project (.dll) and reference it from both applications in the same solution – ASpirin Sep 13 '17 at 07:18
  • Please don't kill me for this but it worked . I feel like a total noob . I was earlier adding the *exe file instead of the *.dll( like you suggested). I just replaced the "exe" that was referencing with the newly created dll and its working. Thank you very much!!!! :) :) – Ananth Kumble Sep 13 '17 at 07:29