我是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在当前上下文中不存在” ..当然会,但是我的问题是如何从上述流中传递值?