WCF可以自动调度吗?

发布于 2024-12-01 18:06:05 字数 285 浏览 0 评论 0原文

我有以下要求:
(1) 当用户请求时执行“操作 A”。
(2) 即使用户没有请求,我们也希望每天执行两次相同的“操作 A”。

我有一个 WCF Web 服务,它具有执行操作 A 的方法 XYZ。当用户请求时,XYZ 方法将被调用。

现在的问题是,我可以在不创建窗口服务(可以托管此服务)或创建代理的情况下安排此操作吗?

有没有一种方法可以仅使用一个应用程序根据用户请求执行操作并安排相同的操作?

I have below requirements:
(1) Perform 'action A' when user requests for it.
(2) We also want to perform the same 'Action A' twice in a day even if users don't request for it.

I have a WCF web service which has method XYZ that performs action A. The method XYZ will be called when user requests for it.

Now question is, can I schedule this action without creating window service (which can host this service) or creating proxy?

Is there a way to perform an action by user request and schedule that same action, using only one application?

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

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

发布评论

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

评论(2

凡尘雨 2024-12-08 18:06:05

不可以,WCF 无法自动计划。您需要实现一个计划任务(请参阅在 Windows 上计划作业),这是一个带有计时器(如果我理解正确的话,您说过您不想这样做)或其他带有计时器的应用程序。

您可以按照其他答案启动一个线程,但这依赖于您的服务调用自身 - 我更喜欢从另一个进程外部调用它。

计划任务可以运行可执行文件。您可以编写一个控制台应用程序来调用 WCF 服务、记录任何结果(如果需要),然后完成。

我通常更喜欢通过 Windows 服务实现这种类型的计时器,只是因为 Windows 服务可以被监视、可以记录、并且可以自动启动/自动重新启动 - 安装它并且它“正常工作”。如果我不想使用 Windows 服务,那么我会安排一个任务。

No, WCF cannot be auto-scheduled. You need to implement a Scheduled Task (see Scheduling jobs on windows), a Windows Service with a timer (which you've said you don't want to do, if I understand correctly) or some other application with a timer.

You could start a thread as per the other answer but this relies on your service calling itself - I'd prefer to call it externally, from another process.

A scheduled task can run an executable. You could write a console application that calls your WCF service, logs any result (if necessary) and then completes.

I normally prefer to implement this type of timer through a Windows Service, simply because the Windows Service can be monitored, can log, and can auto-start / auto-restart - install it and it 'just works'. If I didn't want to use a Windows Service then I'd schedule a task.

好菇凉咱不稀罕他 2024-12-08 18:06:05

我通常通过从某种任务调度程序调用 WCF 服务方法来完成此操作。在一种非常简单的形式中,您可以从服务中生成一个线程,定期运行 WCF 方法。同样,这不是最好的解决方案,但最容易演示。您也可以使用其他一些调度程序库来执行此操作...

[ServiceContract]
public class SomeClass
{
    [ServiceOperation]
    public void SomeServiceMethod() { ... }

然后在应用程序启动的某个位置:

Thread t = new Thread(new ThreadStart(CallService));
t.Start();

...

// this will call the WCF service method once every hour
public void CallService()
{
    Thread.Sleep(3600000); // sleep 1 hour
    new SomeClass().SomeServiceMethod();
}

这是一种方法,虽然不是最好的方法,但基本上您可以像任何其他方法一样调用 WCF 服务方法在应用程序中。

I typically do this by just calling the WCF service method from some kind of task scheduler. In a really simple form, you could just spawn a Thread from your service, that runs the WCF method periodically. Again this isnt the best solution, but its easiest to demonstrate. You could use some other scheduler library to do this too...

[ServiceContract]
public class SomeClass
{
    [ServiceOperation]
    public void SomeServiceMethod() { ... }

Then somewhere in the application startup:

Thread t = new Thread(new ThreadStart(CallService));
t.Start();

...

// this will call the WCF service method once every hour
public void CallService()
{
    Thread.Sleep(3600000); // sleep 1 hour
    new SomeClass().SomeServiceMethod();
}

This is one way to do it, although not the best way, but basically you can just call the WCF service method just like any other method in the application.

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