如何在Quartz.net Scheduler中的Execute方法中调用方法

我是Quartz.Net的初学者。我试图从我的quartz.net计划作业执行方法中调用一个方法。任何人都可以提供帮助,如果这是正确的方法还是有更好的方法可用?

[HttpPost]
public actionResult Index(Tweet twts,httppostedfileBase imgFile) 
{
    UploadFile uploadFile = new UploadFile();
    bool isPosted = uploadFile.UploadFiles(twts,imgFile);      

    if(isPosted)
    {
        twts.tweets = "";
        ViewBag.Message = "Tweeted Successfully";
    }
    else
    {
        ViewBag.Message = "Tweet Post Unsuccessful";
    }

    return View("Tweets");
}

UploadFile.cs

public bool UploadFiles(Tweet twts,httppostedfileBase imgFile)
{
    string key = Utils.Twitterkey;
    string secret = Utils.Twittersecret;
    string token = Utils.Twittertoken; 
    string tokenSecret = Utils.TwittertokenSecret;

    string message = twts.tweets;

    string filePath; string imagePath = "";

    httppostedfileBase filebase =
        new httppostedfileWrapper(HttpContext.Current.Request.Files["imgFile"]);

    if (imgFile == null)
    {
        imgFile = filebase;
    }

    if (imgFile.FileName != "")
    {

        filePath = HttpContext.Current.Server.MapPath("~/Images/");

        if (!Directory.Exists(filePath))
        {
            Directory.CreateDirectory(filePath);
        }

        filePath = filePath + Path.GetFileName(imgFile.FileName);
        imgFile.SaveAs(filePath);
        imagePath = 
            Path.Combine(HttpContext.Current.Server.MapPath(@"~/Images/"),filePath);
    }
            //Enter the Image Path if you want to upload image.

    var service = new TweetSharp.TwitterService(key,secret);
    service.AuthenticateWith(token,tokenSecret);

    //this Condition  will check weather you want to upload a image & text or only text 
    if (imagePath.Length > 0)
    {
        using (var stream = new FileStream(imagePath,FileMode.Open))
        {
            var result = service.SendTweetWithMedia(
                new SendTweetWithMediaOptions
                {
                    Status = message,Images = new Dictionary<string,Stream> { { "sos",stream } }
                });
        }
    }
    else // just message
    {
        var result = service.SendTweet(new SendTweetOptions
        {
            Status = message
        });
    }

    return true;
}

JobScheduler.cs

public class JobScheduler<IDGJob>
        where IDGJob : Quartz.IJob
{
    public static void Start()
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
        scheduler.Start();
        IJobDetail job = JobBuilder.Create<IDGJob>().Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("IDGJob","IDG")
            .WithCronSchedule("0 0 12 1/1 * ? *") 
            .StartAt(DateTime.UtcNow)
            .WithPriority(1)
            .Build();

        scheduler.ScheduleJob(job,trigger);
    }
}

IDGJob.cs

public class IDGJob : IJob
{
    action<Tweet,httppostedfileBase> upFile;

    public IDGJob(action<Tweet,httppostedfileBase> UploadFiles)
    {
        if (UploadFiles == null)
        {
            throw new ArgumentNullException(nameof(UploadFiles));
        }

        action<Tweet,httppostedfileBase> upFile = UploadFiles;
    }

    public void Execute(IJobExecutionContext context)
    {
        upFile(twts,imgFile); ****
    }
}

基于以下答案,我更新了此类。 ****引发错误“ twts在当前上下文中不存在” ..当然会,但是我的问题是如何从上述流中传递值?

la_zhanghui 回答:如何在Quartz.net Scheduler中的Execute方法中调用方法

您的IDGJob类型应进行回调并在Execute()内部调用它

public class IDGJob : IJob
{
    Action callback;

    public IDGJob(Action action)
    {
        if(action == null)
            throw new ArgumentNullException(nameof(action));

        callback = action;
    }

    public void Execute(IJobExecutionContext context)
    {
        callback();
    }
}

现在,使用此类的代码可以在构造对象并执行所需的操作时提供回调。

或者,您可以公开事件Executed并在客户端代码中绑定处理程序。

public class IDGJob : IJob
{
    public event EventHandler Executed;

    public void Execute(IJobExecutionContext context)
    {
        Executed?.(this,EventArgs.Empty);
    }
}

在此版本中,客户端代码将在+=Executed

本文链接:https://www.f2er.com/3031352.html

大家都在问