开发者

Windows Service Idea?

Let me explain the scenario and what I am trying to accomplish.

Scenario: I have a web application that collects a date (ex 07/12/201开发者_开发知识库1) and a time (ex 07:45PM) and store them into database (SQL).

What I am trying to do: At 07:45PM on 07/12/2011, I want to call a web service to run another job.

I am thinking about building a windows service that runs every 15 minutes everyday, gathers all the "pending" requests (dates and times), queues them up, and executes the requests in that order.

Please feel free to provide any other approach for this.


In the past when I have done this I use the Windows Task Scheduler to run the exe that does what I want.

For what you are wanting to do a windows service seems like overkillm, I typically just create a basic console app that does what I need. With the Task Scheduler you can specify exactly when you want to run it and you are done.


Windows Services add a (sometimes) unnecessary level of complexity to a problem like this.

I would recommend starting with a simple console application and using Windows Scheduler to run it every x minutes.

If you decide to convert to a "real" service at a later time almost all of your code should be reusable.


You could evaluate the following solutions before writing out a windows service.

http://www.firedaemon.com/ - FireDeamon provides a free version for scheduling jobs. http://quartznet.sourceforge.net/ - An open source scheduling library, good to go for if your windows service need to support more features.

If you are working on .NET Framework 4, this link should shed some light on this issue.


I've used Quartz.Net with some success. It's a bit more flexible than you've described. Scheduling a new task is as easy as:

    public static void Schedule(DateTime when, string applicationId)
    {
        ISchedulerFactory factory = new StdSchedulerFactory();
        IScheduler scheduler = factory.GetScheduler();

        JobDetail jobDetail = new JobDetail("Realization Job", null, typeof(CustomTask));
        jobDetail.JobDataMap["applicationId"] = applicationId;

        Trigger trigger = new SimpleTrigger("Custom Task Trigger", DateTime.UtcNow, null, 0, TimeSpan.Zero);
        scheduler.ScheduleJob(jobDetail, trigger);
    }

Note that I have a wrapper around the JobScheduler that allows it to act as a Windows service. Creating this as a Windows service allows me to have more robust error handling and does not force me to rely on the OS like the Task Scheduler does.


I've used both Windows Task Scheduler and Windows Services in different web projects, both have their merits.

Personally, I prefer using scheduled tasks. Usually, I'll have a small generic tool calling a URL in the main web application. Sort of like a web service call. The output is appended to a log file.
The benefit of this setup is that if you deploy a new version of the web application, the service is updated as well.

I'd recommend a Windows Service only if you have to perform long-running tasks or tasks that require access to unsafe resources since these don't work well with web applications. Then again, the same sort of tasks could also be performed from a command line tool.

In practice I've found that main problem with Windows Services is the fact they run indefinitely. In a perfect world that's not a problem. In the real world however, I've seen services leaking memory (yes, .NET based services). Over time these services will start to suck up more and more resources.
A scheduled task will start a new process for each invocation, limiting the amount of damage a leaky task can do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜