none
Asynchronous Socket Server failed to start with Windows Service RRS feed

  • Question

  • i have created asynchronous socket server program by refering from https://gist.github.com/leandrosilva/656054, it is working with console program, but failing when running as windows service. Getting below exception. I could not able to trace what went wrong. Any one please help me.

    Application: MIPPluginService.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.NullReferenceException
       at MIPPluginService.MIPService..cctor()
    Exception Info: System.TypeInitializationException
       at MIPPluginService.MIPService..ctor()
       at MIPPluginService.Program.Main()

     
    • Edited by Vasu0289 Tuesday, April 10, 2018 9:40 AM
    Tuesday, April 10, 2018 9:39 AM

All replies

  • In your Program.Main you are creating an instance of MIPService. That is triggering a call to the static constructor for MIPService. That method is referencing a null object and crashing. Since we don't have access to your code you'll have to debug it yourself.

    Without any more information I'm going to wager that MIPService is trying to make a network call and you are not running the service under an account with network access. To access the network you need to run the service either as NETWORK SERVICE or a user account with network access. SYSTEM and LOCAL SERVICE do not have this access.

    Another issue I see is that this is occurring in Program.Main. That is wrong. Program.Main is going to be called when the service process is started, not when your service is started. You should do nothing in Program.Main other than instantiate an instance of your service class. Your service class should do nothing in its constructor either. Any initialization (including dependencies) that a service has should be done in the service's OnStart method. This is the only method that gets called when a service is started. If the user stops your service and restarts it then OnStart is called again. The instance of your service that was initially created never goes away until the service process does and therefore should not hang onto any resources (since the service may not even be running).


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 10, 2018 7:36 PM
    Moderator
  • In your Program.Main you are creating an instance of MIPService. That is triggering a call to the static constructor for MIPService. That method is referencing a null object and crashing. Since we don't have access to your code you'll have to debug it yourself.

    Without any more information I'm going to wager that MIPService is trying to make a network call and you are not running the service under an account with network access. To access the network you need to run the service either as NETWORK SERVICE or a user account with network access. SYSTEM and LOCAL SERVICE do not have this access.

    Another issue I see is that this is occurring in Program.Main. That is wrong. Program.Main is going to be called when the service process is started, not when your service is started. You should do nothing in Program.Main other than instantiate an instance of your service class. Your service class should do nothing in its constructor either. Any initialization (including dependencies) that a service has should be done in the service's OnStart method. This is the only method that gets called when a service is started. If the user stops your service and restarts it then OnStart is called again. The instance of your service that was initially created never goes away until the service process does and therefore should not hang onto any resources (since the service may not even be running).


    Michael Taylor http://www.michaeltaylorp3.net

    Hello Mike,

    I'm not sure I agree with this "Program.Main is going to be called when the service process is started, not when your service is started." it may be technically correct but I think its immaterial. My experience is that Main is called automatically every time a service receives a Start request and runs right before the OnStart runs.

    MS documentation on services has always been somewhat incomplete and there's no clear explanation for when the service process starts.

    Perhaps you are correct and it is only meaningful for a service program that contains multiple services?

    So starting the first service would cause Main to run first then OnStart for the service and starting subsequent services would cause only OnStart to run for each subsequent service started.
    Sunday, May 6, 2018 8:56 PM