我使用c#技术在asp.net 2.0中创建了我的网站。我在我的管理控制部分中包含了一种新闻信件应用程序。当Add_Code目录中有一个JobRunner.cs类时。
实际上我的问题是在新闻信件申请中开始工作。详细信息我还包括我的文件cs.code和JobRunner.cs类的完整代码。
这是我的manage_jobs.aspx.cs编码,用于设置作业ID以启动作业。
protected void lbut_startjob_Click(object sender, EventArgs e)
{
int count = 0;
SetData();
GV_ViewJobs.AllowPaging = false;
this.FillGrid();
ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
count = arr.Count;
for (int i = 0; i < GV_ViewJobs.Rows.Count; i++)
{
if (arr.Contains(GV_ViewJobs.DataKeys[i].Value))
{
StartJob(GV_ViewJobs.DataKeys[i].Value.ToString());
arr.Remove(GV_ViewJobs.DataKeys[i].Value);
}
}
ViewState["SelectedRecords"] = arr;
hfCount.Value = "0";
GV_ViewJobs.AllowPaging = true;
this.FillGrid();
ShowMessageRun(count);
}
private void StartJob(string JobID)
{
int jobId = Convert.ToInt32(JobID);
if (!JobRunner.IsJobQueuedOrRunning(jobId))
{
JobRunner.RunJob(jobId);
}
}
和我的JobRunner.cs类(所有编码)
JobRunner.cs -------------------------------------------- -------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Threading;
/// <summary>
/// Summary description for JobRunner
/// </summary>
public static class JobRunner
{
private static List<JobInfo> ActiveJobs = new List<JobInfo>();
//public static JobRunner()
//{
// //
// // TODO: Add constructor logic here
// //
//}
public static void RunJob(int jobId)
{
lock (ActiveJobs)
{
JobInfo jobInfo = FindQueuedOrRunningJob(jobId);
if (jobInfo != null)
return;
jobInfo = FindExitedJob(jobId);
if (jobInfo == null)
{
jobInfo = new JobInfo(jobId);
ActiveJobs.Add(jobInfo); // Default status is Queued
}
else
{
jobInfo.State = JobInfo.JobState.Queued;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(StartJob), jobInfo);
}
}
public static bool IsJobQueuedOrRunning(int jobId)
{
return FindQueuedOrRunningJob(jobId) != null;
}
public static JobInfo FindJob(int jobId)
{
JobInfo ji = FindQueuedOrRunningJob(jobId);
if (ji != null)
return ji;
return FindExitedJob(jobId);
}
public static JobInfo FindQueuedOrRunningJob(int jobId)
{
lock (ActiveJobs)
{
for (int i = 0; i < ActiveJobs.Count; i++)
{
JobInfo ji = ActiveJobs[i];
if (ji.JobId == jobId && (ji.State == JobInfo.JobState.Queued | ji.State == JobInfo.JobState.Running))
return ji;
}
return null;
}
}
public static JobInfo FindExitedJob(int jobId)
{
lock (ActiveJobs)
{
for (int i = 0; i < ActiveJobs.Count; i++)
{
JobInfo ji = ActiveJobs[i];
if (ji.JobId == jobId && ji.State == JobInfo.JobState.Exited)
return ji;
}
return null;
}
}
public static void StartJob(object stateInfo)
{
JobInfo jobInfo = (JobInfo)stateInfo;
int jobId = jobInfo.JobId;
jobInfo.State = JobInfo.JobState.Running;
// Email settings
string fromName = ConfigurationManager.AppSettings["Newsletter_FromName"];
string fromEmailAddress = ConfigurationManager.AppSettings["Newsletter_FromEmailAddress"];
SmtpClient client = Util.GetSmtpClient();
// For logging
int emailsSentInThisAttempt = 0;
try
{
using (SqlConnection con = Util.GetConnection())
{
con.Open();
jobInfo.Log(con, "Connected to database...");
// Step 1: Retrieving job information
string subject, plainText;
DatabaseHelper.GetJobInfo(con, jobInfo.JobId, out subject, out plainText);
jobInfo.Log(con, String.Format("Retrieved job #{0} information...", jobId));
// Creating a loop to send all emails one-by-one
while (true)
{
// Step 2: Retrieving one email from the list
// to whom this email will be sent
//int subscriberId;
string email;
//int validationCode;
//DatabaseHelper.PopAllotedEmail(con, jobId, out subscriberId,out email, out validationCode);
DatabaseHelper.PopAllotedEmail(con, jobId, out email);
if (email == null)
break;
// Step 3: Sending email
try
{
//SendEmail(fromName, fromEmailAddress, email, subject, plainText, client, subscriberId,validationCode);
SendEmail(fromName, fromEmailAddress, email, subject, plainText, client);
}
catch (Exception ex)
{
DatabaseHelper.PushAllotedEmail(jobId, email);
jobInfo.Log(con,
String.Format("Could not send email to: {0}",
email));
throw ex;
}
emailsSentInThisAttempt++;
}
DatabaseHelper.SetJobCompleted(con, jobId, DateTime.Now);
jobInfo.Log(con, "Successfully delivered " +
emailsSentInThisAttempt + " email(s).");
jobInfo.Log(con, "Exiting with success!");
}
}
catch (Exception ex)
{
jobInfo.Log("Exception: " + ex.Message);
jobInfo.Log("Exiting with disgrace!");
}
finally
{
jobInfo.State = JobInfo.JobState.Exited;
}
}
private static void SendEmail(string fromName, string fromEmailAddress, string toEmailAddress, string subject, string plainText, SmtpClient client)
{
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
client.EnableSsl = true;
if (fromName != null)
msg.From = new MailAddress(fromName + " <" + fromEmailAddress + ">");
else
msg.From = new MailAddress(fromEmailAddress);
msg.To.Add(new MailAddress(toEmailAddress));
msg.Subject = subject;
string pathToApp = ConfigurationManager.AppSettings["Newsletter_PathToApplication"];
string unsubscribeText = "<br/>\r\n\r\n---------------------------\r\n" +
"To unsubscribe, please click the following link:\r\n" +
pathToApp + "Unsubscribe.aspx";
msg.Body = plainText + unsubscribeText;
client.Send(msg);
}
public class JobInfo
{
public enum JobState { Queued, Running, Exited }
public int JobId
{
get
{
return JobId;
}
set
{
JobId = value; // <- Exception occurred here.
}
}
public JobState State
{
get
{
return State;
}
set
{
State = value;
}
}
public JobInfo(int jobId)
{
JobId = jobId;
State = JobState.Queued;
}
public void Log(string message)
{
DatabaseHelper.AddLogMessage(JobId, DateTime.Now, message);
}
public void Log(SqlConnection con, string message)
{
DatabaseHelper.AddLogMessage(con, JobId, DateTime.Now, message);
}
public override bool Equals(object obj)
{
if (obj is JobInfo)
{
JobInfo otherJobInfo = (JobInfo)obj;
if (JobId == otherJobInfo.JobId && State == otherJobInfo.State)
return true;
}
return false;
}
}
}
例外:
“System.StackOverflowException未处理 App_Code.okrls50x.dll中出现未处理的“System.StackOverflowException”类型异常“
请帮助我在JobInfo方法中获取设置属性有问题吗?....
答案 0 :(得分:3)
其中一个属性导致StackOverflowException:
public int JobId
{
get
{
return JobId;
}
set
{
JobId = value;
}
}
public JobState State
{
get
{
return State;
}
set
{
State = value;
}
}
您可以将属性实现为(自动实现):
public int JobId { get; set; }
或者您可以使用支持字段手动实现它:
private int _jobId;
public int JobId
{
get { return _jobId; }
set { _jobId = value; }
}
答案 1 :(得分:1)
问题是代码中的某个地方你有一个属性
JobState State
{
get { return State; }
set { State = value; } //THIS LINE RUNS in STACKOVERFLOW
}
修复此声明变量JobState,如下所示:
private JobState state;
public JobState State
{
get { return state; }
set { state= value; }
}
答案 2 :(得分:0)
public JobState State
{
get
{
return State;
}
set
{
if(value != State)
{
State = value;
}
}
}