Windows 服务理念?

发布于 2024-11-09 10:00:01 字数 270 浏览 0 评论 0原文

让我解释一下这个场景以及我想要完成的任务。

设想: 我有一个网络应用程序,它收集日期(例如 07/12/2011)和时间(例如 07:45PM)并将它们存储到数据库(SQL)中。

我正在尝试做的事情: 2011 年 7 月 12 日晚上 07:45,我想调用 Web 服务来运行另一项作业。

我正在考虑构建一个每天每 15 分钟运行一次的 Windows 服务,收集所有“待处理”请求(日期和时间),将它们排队,并按该顺序执行请求。

请随时为此提供任何其他方法。

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/2011) 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

要走干脆点 2024-11-16 10:00:01

过去,当我这样做时,我使用 Windows 任务计划程序 来运行执行我想要的操作的 exe。

对于您想要做的 Windows 服务似乎有些过头了,我通常只是创建一个基本的控制台应用程序来完成我需要的事情。使用任务计划程序,您可以准确指定何时运行它并完成。

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.

心是晴朗的。 2024-11-16 10:00:01

Windows 服务(有时)会为此类问题增加不必要的复杂性。

我建议从一个简单的控制台应用程序开始,并使用 Windows Scheduler 每 x 分钟运行一次。

如果您决定稍后转换为“真正的”服务,那么几乎所有代码都应该是可重用的。

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.

感情洁癖 2024-11-16 10:00:01

在编写 Windows 服务之前,您可以评估以下解决方案。

http://www.firedaemon.com/ - FireDeamon 提供用于调度作业的免费版本。
http://quartznet.sourceforge.net/ - 一个开源调度库,如果您的 Windows 是一个不错的选择服务需要支持更多的功能。

如果您正在使用 .NET Framework 4,则此 链接 应该可以阐明这个问题。

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.

那一片橙海, 2024-11-16 10:00:01

我使用 Quartz.Net 取得了一些成功。它比您描述的更灵活一些。安排新任务非常简单:

    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);
    }

请注意,我有一个围绕 JobScheduler 的包装器,允许它充当 Windows 服务。将其创建为 Windows 服务使我能够进行更强大的错误处理,并且不会强迫我像任务计划程序那样依赖操作系统。

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.

凉栀 2024-11-16 10:00:01

我使用过Windows 任务计划程序 和Windows Services在不同的web项目中,都有各自的优点。

就我个人而言,我更喜欢使用计划任务。通常,我会使用一个小型通用工具来调用主 Web 应用程序中的 URL。有点像网络服务调用。输出将附加到日志文件中。
此设置的好处是,如果您部署新版本的 Web 应用程序,服务也会更新。

仅当您必须执行长时间运行的任务或需要访问不安全资源的任务时,我才建议使用 Windows 服务,因为这些任务不适用于 Web 应用程序。
不过,同样类型的任务也可以通过命令行工具执行。

在实践中,我发现 Windows 服务的主要问题是它们无限期地运行。在完美的世界里这不是问题。然而,在现实世界中,我见过服务泄漏内存(是的,基于 .NET 的服务)。随着时间的推移,这些服务将开始占用越来越多的资源。
计划任务将为每次调用启动一个新进程,从而限制泄漏任务可能造成的损害程度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文