Wrapping a C# service in a console app to debug it

前端 未结 10 1208
遇见更好的自我
遇见更好的自我 2020-12-15 03:36

I want to debug a service written in C# and the old fashioned way is just too long. I have to stop the service, start my application that uses the service in debug mode (Vis

相关标签:
10条回答
  • 2020-12-15 04:38

    Here is a blog post about running your windows service as a console app.

    You could also just create a new Console Application that references the same logic as your service to test methods, or set up Unit Tests on your services' logic

    0 讨论(0)
  • 2020-12-15 04:39

    TopShelf is another project that is perfect for this approach. It allows you to run a process as a service, or as a regular console application with minimal configuration.

    0 讨论(0)
  • 2020-12-15 04:40

    I tend to have either a config setting or use a directive for debug builds:

     #if DEBUG
        Debugger.Break();
     #endif
    

    or

    if(Settings.DebugBreak)
                Debugger.Break();
    

    I put that in the OnStart method of the service component. Then you are prompted automatically and attached to the process.

    0 讨论(0)
  • 2020-12-15 04:40

    I have used unit tests to debug difficult setups in the past, just write a unit test that calls whatever service method with whatever parameters and set debug breakpoints in the unit test.

    Using testdriven.net or jetbrains testrunner makes it easier.

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