Running Windows Service Application without installing it

后端 未结 6 1870
旧巷少年郎
旧巷少年郎 2021-01-11 16:55

Whem I\'m writing a Windows Service and just hit F5 I get the error message that I have to install it using installutil.exe and then run it. In practice this me

相关标签:
6条回答
  • 2021-01-11 17:26

    The best way in my opinion is to use Debug directive. Below is an example for the same.

    #if(!DEBUG)
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
             // Calling MyService Constructor 
                new MyService() 
        };
         ServiceBase.Run(ServicesToRun);
    #else
      MyService serviceCall = new MyService();
      serviceCall.YourMethodContainingLogic();
    #endif
    

    Hit F5 And set a Breakpoint on your YourMethodContainingLogic Method to debug it.

    0 讨论(0)
  • 2021-01-11 17:26

    You can write this code in program.cs

    //if not in Debug
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
       new MyService() 
    };
    ServiceBase.Run(ServicesToRun);
    
    //if debug mode
    MyService service = new MyService();
    service.OnDebug();
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    

    in MyService class

    public void OnDebug()
    {
       OnStart(null);
    }
    
    
    
    0 讨论(0)
  • 2021-01-11 17:30

    I usually put the bulk of the service implementation into a class library, and then create two "front-ends" for running it - one a service project, the other a console or windows forms application. I use the console/forms application for debugging.

    However, you should be aware of the differences in the environment between the debug experience and when running as a genuine service - e.g. you can accidentally end up dependent on running in a session with an interactive user, or (for winforms) where a message pump is running.

    0 讨论(0)
  • 2021-01-11 17:40

    You can use Environment.UserInteractive variable. Details of implementation here

    0 讨论(0)
  • 2021-01-11 17:43

    You cannot run Windows Service as say another console or WinForms application. It needs to be started by Windows itself.

    If you don't have infrastructure ready to use as @Damien_The_Unbeliever suggests (which is what I recommend as well) you can install the service from the debug location. So you use installutil once and point it to executable located in /bin/debug. Then you start a service from services.msc and use Visual Studio > Debug > Attach to Process menu and attach to the Windows service.

    You can also consider using Thread.Sleep(10000) as the first line in the OnStart call, or Debugger.Break() to help you out to be able to attach before the service executes any work. Don't forget to remove those before the release.

    0 讨论(0)
  • 2021-01-11 17:43

    Here's an easy way I use to debug Windows service applications without installing them, starting via Windows Service Control Manager, attaching to debuggers, etc. The following is in VB but hopefully you get the idea.

    In this example, TestService's main class is named svcTest.vb.

    Within Shared Sub Main() inside svcTest.Designer.vb, the default code looks something like this:

    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New svcTest}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
    

    Comment everything out within Main() and add the following 2 lines of code.

    Dim objSvc As New svcTest()
    objSvc.OnStart(Nothing)
    

    Now just set a breakpoint where you want to start debugging, hit F11 to step into the code, and then proceed as normal as if you were working with a standard desktop application. When you're finished debugging, simply reverse the changes made within Main().

    This was done using Visual Studio Enterprise 2017 on Windows Server 2012 R2.

    0 讨论(0)
提交回复
热议问题