Reputation: 753
I'm developing a windows service in C# .Net and i'm having some dificulty to execute and test my service. I'm using a timer component to run it from time to time and execute my methods. I have to intialize the service all the time to run it. Anyone know a more practical way to test a service?
Upvotes: 8
Views: 21598
Reputation: 183
After create window service you can set it into window service.
Install service using InstallUtil.exe To install or uninstall windows service (which was created using .NET Framework) use utility InstallUtil.exe. This tool can be found in the following path (use appropriate framework version number).
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe
To install .NET service run command similar to this (specify full path to your service).
InstallUtil.exe "path of service"
To uninstall .NET service use the same command with parameter /u.
InstallUtil.exe /u "path of service"
Upvotes: 1
Reputation: 12314
You probably want to add unit tests to your service.
It sounds like you could benefit more from adding your application to the Task Scheduler instead of running it as a service.
Other than that if you really need it as a service you need to design it so it can be tested. I usually write my services in a separate class and add a .EXE project to it so I can run it from the command line too.
Upvotes: 5
Reputation: 1166
One of the ways I have run services when developing is along these lines - Run a windows service as a console. I have found it useful because you can add code to write out to the console for debug information and writing out any relevant exception data.
I would not use this method instead of unit tests but I found this to be a useful way to work and debug where needed.
Upvotes: 4
Reputation: 54764
Windows services are ordinary EXEs that happen to connect to the service control manager on startup.
I normally test services by giving them an optional command line argument that tells them to execute normally, inside the Main
method, instead of acting as a service. That way I can debug them directly within Visual Studio, or on the command line, etc.
Upvotes: 10
Reputation: 158349
This often boils down to the question what you want to test. Do you want to test the service, or the functionality that it performs? Personally I would...
...and then keep the amount of code in the service itself to the bare minimum needed to trigger the functionality in the class library.
Upvotes: 16