简体   繁体   English

如何为我的ASP.Net Web应用程序测试Windows Service Scheduler?

[英]How to test windows service scheduler for my ASP.Net Web Application?

I created a scheduler for my web app, which is setup to be fired every 3 seconds. 我为我的Web应用程序创建了一个调度程序,该调度程序设置为每3秒触发一次。 I want to use Console.Write("bob") to see if the scheduler is working properly, but nothing happened, only the web page is created after I click the 'debug' button. 我想使用Console.Write(“ bob”)来查看调度程序是否正常运行,但是没有任何反应,仅在单击“调试”按钮后创建了网页。 Am I testing the wrong way or the scheduler is indeed not working? 我是在测试错误的方式还是调度程序确实无法正常工作?

Here's the code I wrote: 这是我写的代码:

namespace Scheduler
{
    public partial class CheckExpireService : ServiceBase
    {
        private CheckExpireJob job;
        private Timer stateTimer;
        private TimerCallback timerDelegate;


        public CheckExpireService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            job = new CheckExpireJob();
            timerDelegate = new TimerCallback(job.CheckExpireApplicants);
            stateTimer = new Timer(timerDelegate, null, 1000, 3000);
        }

        protected override void OnStop()
        {
            stateTimer.Dispose();
        }
    }
}


namespace Scheduler
{
    class CheckExpireJob: ServiceBase
    {
        public void CheckExpireApplicants(object stateObject) {
        Console.Write("bob");
        }
    }
}

Thanks in advance!! 提前致谢!!

(I was going to leave a comment, but edit and answer are the only things I seem to have available.) (我本来要发表评论,但是编辑和回答是我似乎唯一可以使用的功能。)

First, I don't think you can run a Windows Service in Debug mode...it has to be compiled and installed as a service and started and stopped with the services manager in Windows. 首先,我认为您不能在调试模式下运行Windows服务...必须将其编译并安装为服务,并使用Windows中的服务管理器启动和停止。 If you have this code somewhere in a web application, it will not work. 如果您在Web应用程序中的某处拥有此代码,它将无法正常工作。 It needs to be a seperate project. 它需要一个单独的项目。

Second, you can't construct and call a service like this. 其次,您不能像这样构造和调用服务。 If you want a service that does something on a timed interval, you need to have the timer and all of its workings within the service. 如果您希望某个服务在一定时间间隔内执行某项操作,则需要在该服务中包含计时器及其所有功能。

You can't schedule a web app...it runs when someone visits the page. 您无法安排网络应用程序...它会在有人访问该页面时运行。 What is it that you REALLY need to schedule? 您真正需要安排的是什么? Regular updates of the data that the web app will pull from? 定期更新Web应用程序将要提取的数据?

Need to know more about what you are trying to accomplish to be any help. 需要更多有关您要完成的工作的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM