The meaning of what. I create an absolutely empty service:

projectinstaller.cs

using System; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; namespace mrserivce { [RunInstaller(true)] public class ProjectInstaller : Installer { private ServiceProcessInstaller serviceProcessInstaller; private ServiceInstaller serviceInstaller; public ProjectInstaller() { serviceProcessInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalService; serviceInstaller.ServiceName = MainService.MyServiceName; this.Installers.AddRange(new Installer[] { serviceProcessInstaller, serviceInstaller }); } } } 

mainservice.cs

 using System; using System.ServiceProcess; namespace mrserivce { public class MainService : ServiceBase { public const string MyServiceName = "mrserivce"; public MainService() { InitializeComponent(); } private void InitializeComponent() { this.ServiceName = MyServiceName; } protected override void OnStart(string[] args) { } protected override void OnStop() { } } } 

kernel.cs

 using System; using System.ServiceProcess; namespace mrserivce { static class Kernel { static void Main() { ServiceBase.Run(new ServiceBase[] { new MainService() }); } } } 

Next, I compile it in Release, open cmd from the admin, use cd to go to the directory with the executable, and write:

 sc create testService binPath=mrservice.exe type=own start=auto sc start testService 

After that (instantly) an error occurs:

 [SC] StartService: ошибка: 1053: Служба не ответила на запрос своевременно. 

What is the problem?

  • Try installing via InstallUtil.exe - Exodium
  • The difference is not really big. Unless through InstallUtil in one assembly several services can work. - Eduard Design
  • ProjectInstaller is superfluous in question - sc.exe does not use it - Pavel Mayorov
  • @PavelMayorov And then I think, why sc needs to indicate both the user, and the description and name, if all this is written in the installer. Thanks, I will install via InstallUtil. - Eduard Design
  • @EduardGushchin is not needed, sc.exe is more powerful - Pavel Mayorov

2 answers 2

In fact, the problem was in the confusion with the file system. As you know, because of the transition to x64, all 32-bit assemblies when accessing system32 will redirect to SysWOW64. Therefore, when the assembly with the service is in system32, when you install sc, it starts looking for a service in SysWOW64 (of course, there is none). Because of what this error occurs.

  • one
    It remains to understand why there was a copy of the assembly in system32 ... :-) - Pavel Mayorov
  • one
    Persuade the customer is meaningless :) - Eduard Design

Try adding Thread or Task to the onStart method.

 protected override void OnStart(string[] args) { Thread thread = new Thread(new ThreadStart(StartFunction)); thread.Start(); } private void StartFunction() { // какой-то код }