在一个asp.net网站Quartz.net设置 [英] Quartz.net setup in an asp.net website

查看:153
本文介绍了在一个asp.net网站Quartz.net设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚加入quartz.net DLL到我的bin并开始了我的例子。如何调用使用基于时间quartz.net一个C#方法?

I've just added quartz.net dll to my bin and started my example. How do I call a C# method using quartz.net based on time?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quartz;
using System.IO;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(SendMail())
             Response.write("Mail Sent Successfully");
    }
   public bool SendMail()
   {
    try
    {
        MailMessage mail = new MailMessage();
        mail.To = "test@test.com";
        mail.From = "sample@sample.com";
        mail.Subject = "Hai Test Web Mail";
        mail.BodyFormat = MailFormat.Html;
        mail.Body = "Hai Test Web Service";
        SmtpMail.SmtpServer = "smtp.gmail.com";
        mail.Fields.Clear();
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "redwolf@gmail.com");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "************");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
        SmtpMail.Send(mail);
        return (true);
    }
    catch (Exception err)
    {
        throw err;
    }
}
}

在这里,我只是发送页面加载邮件。我怎么叫的SendMail()一旦在给定时间(比如清晨六点)每天使用quartz.net?我不知道如何开始。我应该配置它在我的的Global.asax 文件?任何建议?

Here I am just sending a mail on page load. How do I call SendMail() once in a day at a given time (say 6.00 AM) using quartz.net? I don't know how to get started. Should I configure it in my global.asax file? Any suggestion?

推荐答案

你有没有尝试 quartz.net教程

由于您的Web应用程序可能会被回收/重新启动,你应该(再)intialize在Global.asax.cs中的的Application_Start处理程序quartz.net调度。

Since your web app might get recycled/restarted, you should probably (re-)intialize the quartz.net scheduler in the Application_Start handler in global.asax.cs.

更新(完整的例子和其他一些因素):

Update (with complete example and some other considerations):

下面是一个完整的例子如何使用quartz.net做到这一点。首先,你必须创建一个实现了由quartz.net定义的 IJob 接口的类。这个类是由quartz.net调度在TNE配置的时间打来电话,因此应包含您发送邮件的功能:

Here's a complete example how to do this using quartz.net. First of all, you have to create a class which implements the IJobinterface defined by quartz.net. This class is called by the quartz.net scheduler at tne configured time and should therefore contain your send mail functionality:

using Quartz;
public class SendMailJob : IJob
{
    public void Execute(JobExecutionContext context)
    {
        SendMail();
    }
    private void SendMail()
    {
        // put your send mail logic here
    }
}

接下来,您必须初始化quartz.net调度06:00调用你的​​工作,每天一次。这可以在的Application_Start 来完成的的Global.asax

using Quartz;
using Quartz.Impl;

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();
        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();
        // construct job info
        JobDetail jobDetail = new JobDetail("mySendMailJob", typeof(SendMailJob));
        // fire every day at 06:00
        Trigger trigger = TriggerUtils.MakeDailyTrigger(06, 00);
        trigger.Name = "mySendMailTrigger";
        // schedule the job for execution
        sched.ScheduleJob(jobDetail, trigger);
    }
    ...
}

就是这样。你的工作应该每天06:00执行。为了进行测试,您可以创建触发每分钟(比如)的触发器。看一看的方法 TriggerUtils

虽然上述解决方案可能为你工作,有一件事你应该考虑:你的Web App将得到回收/如果有一段时间(即没有活动用户)没有活动停止。这意味着你发送邮件的功能可能不被执行(仅当有一圈时,邮件应该发送的时候一些活动)。

While the above solution might work for you, there is one thing you should consider: your web app will get recycled/stopped if there is no activity for some time (i.e. no active users). This means that your send mail function might not be executed (only if there was some activity around the time when the mail should be sent).

因此​​,你应该想想你的问题的其他解决方案:

Therefore you should think about other solutions for your problem:


  • 您可能想实现一个窗口服务发送您的电子邮件(Windows服务将始终运行)

  • 或更容易:实现一个小型控制台应用程序的发送邮件的功能,并成立了计划任​​务窗口中在规定的时间来调用您的控制台应用程序,每天一次。

这篇关于在一个asp.net网站Quartz.net设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆