Search code examples
c#.netwindowsservicevirtual-machine

windows service from C# working in local but not in distant desktop


I am currently working on visual studio C# to make a windows service.

I exported the project as "Release" "Any Core" and tried it on my computer, and it worked fine using :

  • sc create myService binPath= C:/myDocs/theService.exe
  • net start myService

I then copied it to my virtual machine (itself on a server that I connect to using distant desktop)

when doing the same commands on the virtual machine it tells me

the service is not responding to the control function
error code 2186

when opening the services maintenance window and manually starting it I get

the service did not respond to the start or control request in a timely fashion
error code 1053

The two errors are different, but what's crazy is that it works on my computer just fine...

What I've tested / checked:

  • the .net version of the project and the vm are both 4.7
  • I have added some try catch writing to a log file in the constructor and OnStart functions, but nothing is outputed.
  • the service is ran as "localUser" on the vm with all rights, like on my computer
  • I tried restarting the vm a couple times

Solution

  • trying to open a file at a location that do not exist apparently do weard stuff on windows :

    • no exception catch
    • service stop responding
    • service returned error is strange

    the error come from here:

    string path = "C:/myDocs/myService.exe";
    if (!File.Exists(path))
    {
        using (FileStream fs = File.Create(path))
        {
        }
    }