Easiest way to develop/debug a Windows service

Sql*_*yan 4 .net vb.net debugging windows-services

I'm developing a Windows Service in VB.NET 2008, but I feel like I'm going to have an aneurysm. To debug the service, I've added a 15 second wait to the initialization code, which gives me time to start the service and attach the .NET debugger before anything happens, so I can hit breakpoints and such. I really miss "integrated" debugging with this type of workaround, and testing seems to be a huge pain.

What's the best way to do "regular" debugging of a Windows Service that's in development? One option I'd considered was moving all of my logic to a DLL project, leaving only control logic in the service itself, and then creating a Forms project that essentially just had "start" and "stop" buttons on it, and called into the DLL to do everything else. This way, I can debug my code normally, and then just deploy the compiled service when the DLLs are ready.

Does this make sense/annoy others? I'm open to any workarounds available. PB's suggestion here sounds like what I'm asking - has anybody used this approach?

sgm*_*ore 13

如果你能处理一点C#,这就是我这样做的方式.

假设你有一个使用onStart方法派生自ServiceBase的类MainService,那么当没有在调试器内运行时,服务正常启动,否则手动调用onStart,它以控制台模式运行代码.

static void Main(string[] args)
{
       // If no command line arguments, then run as a service unless we are debugging it.
       if ( args.Length == 0) 
       {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                 System.Diagnostics.Debugger.Break();
                 args = new string[] { "/NonService"} ;
            }
          else
                 args = new string[] { "/Service"} ;
       }

       string cmdLine = args[0].ToLower().Substring(1);
       Console.WriteLine("Starting Program with Cmdline : " + args[0]);
       switch (cmdLine)
       {
           case "service" :
               ServiceBase.Run(new MainService());
               break;

           case "nonservice" :
               MainService ms = new MainService();
               ms.OnStart(null);
               break;

         ...

         default :
            Console.Error.WriteLine("Unknown Command line Parameter");
            Console.Error.WriteLine("Supported options are /Install /Uninstall /Start /Stop /Status /Service and /NonService");
         }
Run Code Online (Sandbox Code Playgroud)

  • 是的它是控制台应用程序. (2认同)

Rob*_*ine 11

除了使用Debugger.Break()之外,其他几个人已经提到过,我将所有的服务代码都单独汇编到实际的Windows服务项目中.然后我还编写了一个调用相同服务代码的Windows控制台应用程序.这让我只需将控制台应用程序设置为启动项目即可在IDE中进行调试.

Windows服务项目和Windows控制台应用程序本身除了调用服务"核心"代码之外什么都不做,因此由于服务和控制台应用程序之间的差异导致的缺陷范围很小.


Jas*_*ans 6

When I develop a Windows service using .NET, I take advantage of unit tests plus TypeMock so that I can run the code of the service in a unit test, without having to attach to a running instance of the service. Other mocking frameworks you could look at include Rhino Mocks.

So my plan was to use MSTest to create the unit test project and test methods that run against my service, and any run-time dependencies would be handled by TypeMock, which would create mock objects for my service to use. So if my service was handling stuff to do with files, for example, then I could create a mock file, using TypeMock, and use that in my unit test to pass to the service.

In the past, I went through the pain of compiling the service, installing it, running it and attaching, etc. When I discovered mock frameworks, it was such a great feeling being able to test my code with a single click of a button from the Visual Studio IDE.

Give it a try.