views:

85

answers:

4

I’m currently writing a windows service that’s sole purpose in life is to poll a database and, based on the resulting information, update some other data. I wrote this as a windows service because it seemed an ideal platform. I don’t need any user interaction.

However, while developing it, I’ve noticed one or two issues that make developing a windows service more time consuming that developing a straightforward windows app. Has anyone has any experience with this kind of choice? What is the best practice for this kind of app? Are there any reasons why using a windows service is preferable?

+5  A: 

The issues of development imoho are far out weighed by the features that services provide. Remote polling, no need for a user to log on, built in fault recovery and monitoring.

rerun
Agreed, once properly set up, a service can just get the job done without having to worry about anything else.
alex
+1  A: 

I just made my first service too and noticed they are not quite as easy to debug or test. You can install and start the service then attach the debugger to the process. Or you can run the service as a console app just to test out functionality. Something like this http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/

Whether a service is preferable or not is your call. rerun listed good reasons for services. Based on your description it sounds like a scheduled task of some SQL would accomplish what you want.

nportelli
+1  A: 

To debug a windows service directly under visual studio , Add this Code snippet to your service designer class under Main() .

Shared Sub Main()
    #If DEBUG Then
        Dim service As New YourServiceClass
        service.Execute()
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
    #Else
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        'More than one NT Service may run within the same process. To add
        'another service to this process, change the following line to
        'create a second service object. For example,

        ServicesToRun = New System.ServiceProcess.ServiceBase() {New YourServiceClass}

        System.ServiceProcess.ServiceBase.Run(ServicesToRun)

    #End If
End Sub

This is vb.net code but C# should do the same trick. I have managed several windows services like this without any issues. If debug should be preceded with a hash.

PradeepGB
Thanks ChrisF. When I pasted code onto answer windows hash disappeared from code snippet. That's why I had to add a comment that If debug should be preceded with a hash
PradeepGB
A: 

One thing you should be aware of is that a service will not be able to use video hardware acceleration. If you are not doing any graphical work then you can safely ignore the limitation, but if you have graphical-intensive operations (for ex. WPF-related) you have to keep this in mind.

I admit it's not a common scenario, but I've worked on a project where a service rendered WPF controls.

The reason for this is Session0 isolation, described here.

Francesco De Vittori