How do I debug Windows services in Visual Studio?

后端 未结 17 780
梦毁少年i
梦毁少年i 2020-11-28 03:56

Is it possible to debug the Windows services in Visual Studio?

I used code like

System.Diagnostics.Debugger.Break();

but it is givi

17条回答
  •  误落风尘
    2020-11-28 04:14

    Use the following code in service OnStart method:

    System.Diagnostics.Debugger.Launch();
    

    Choose the Visual Studio option from the pop up message.

    Note: To use it in only Debug mode, a #if DEBUG compiler directive can be used, as follows. This will prevent accidental or debugging in release mode on a production server.

    #if DEBUG
        System.Diagnostics.Debugger.Launch();
    #endif
    

提交回复
热议问题