简体   繁体   English

如何在Asp.net网站中添加程序集引用以使用Quartz?

[英]How to add assembly reference to use Quartz in Asp.net Website?

I am working on a website in VS 12 and I need to schedule some task when the application starts for that I am using quartz 2.0.0 but I am getting the following error: 我正在VS 12中的网站上工作,我需要在应用程序启动时安排一些任务,因为我使用的是石英2.0.0,但出现以下错误:

在此处输入图片说明

My website is hosted online. 我的网站是在线托管的。

Job scheduler class: 作业调度程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;


public class jobscheduler
{
    public static void Start()
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create<Class2>().Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
            .Build();

        scheduler.ScheduleJob(job, trigger);
    }
}

The job which I want to schedule: 我要安排的工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using System.Data;
using System.Data.SqlClient;

public class Class2: IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Class1 obj = new Class1();
        DataSet ds;
        ds = new DataSet();
        ds = obj.selecturls();    

        Random ran = new Random();
        int index = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add1 = ds.Tables[0].Rows[index]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add1"] = add1;
        int index2 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add2 = ds.Tables[0].Rows[index2]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add2"] = add2;
        int index3 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add3 = ds.Tables[0].Rows[index3]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add3"] = add3;
        int index4 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add4 = ds.Tables[0].Rows[index4]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add4"] = add4;
        int index5 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add5 = ds.Tables[0].Rows[index5]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add5"] = add5;
        int index6 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add6 = ds.Tables[0].Rows[index6]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add6"] = add6;
        int index7 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add7 = ds.Tables[0].Rows[index7]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add7"] = add7;
        int index8 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add8 = ds.Tables[0].Rows[index8]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add8"] = add8;
        int index9 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add9 = ds.Tables[0].Rows[index9]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add9"] = add9;
        int index10 = ran.Next(ds.Tables[0].Rows.Count - 1);
        string add10 = ds.Tables[0].Rows[index10]["urls"].ToString();
        System.Web.HttpContext.Current.Session["add10"] = add10;
    }
}

Global.asax file: Global.asax文件:

<%@ Application Language="C#" %>
<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        jobscheduler.Start();
        // Code that runs on application startup
    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
    }
</script>

and I don't have any assemblyinfo.cs file because I selected an empty website instead of the new project. 而且我没有任何assemblyinfo.cs文件,因为我选择了一个空网站而不是新项目。

If you downloaded Quartz 2.0.0 from the Internet you have to check dll's properties (right click on the dll in windows explorer -> Properties). 如果您从互联网上下载了Quartz 2.0.0,则必须检查dll的属性(在Windows资源管理器->属性中右键单击该dll)。 It's possible that you have to unblock the library: 您可能必须取消阻止库:

在此处输入图片说明

Another possible solutions you can find in that thread . 您可以在该线程中找到另一种可能的解决方案。

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

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