Running Windows Service Application without installing it

后端 未结 6 1940
旧巷少年郎
旧巷少年郎 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: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.

提交回复
热议问题