我调试windows服务,当我到达while(rdr.read())它没有进入块代码实现它直接进入构造函数并获取数据这里是我的项目:
主要项目:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Transactions;
using newsService.Entities;
namespace newsService
{
public partial class newsService : ServiceBase
{
protected System.Timers.Timer feedGathererTimer;
public newsService()
{
InitializeComponent();
feedGathererTimer = new System.Timers.Timer();
feedGathererTimer.Interval = 600000; //900000;
feedGathererTimer.Enabled = false;
feedGathererTimer.Elapsed += new ElapsedEventHandler(feedGathererTimer_Elapsed);
}
private void feedGathererTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// Get the list of feeds that we will poll
try
{
List<FeedMidSpor> feedListMidSpor = FeedMidSpor.GetList();
List<FeedLebSpor> feedListLebSpor = FeedLebSpor.GetList();
List<FeedWodSpor> feedListWodSpor = FeedWodSpor.GetList();
foreach (FeedMidSpor feed in feedListMidSpor)
{
List<PostMidSpor> postListMidSpor = PostMidSpor.GetNewRssPostsFromUrl(feed);
using (TransactionScope ts = new TransactionScope())
{
if (feed.LastPostGuidNameChanged)
feed.Update();
if (postListMidSpor.Count > 0)
foreach (PostMidSpor post in postListMidSpor)
post.Add();
ts.Complete();
}
}
foreach (FeedLebSpor feed in feedListLebSpor)
{
List<PostLebSpor> postListLebSpor = PostLebSpor.GetNewRssPostsFromUrl(feed);
using (TransactionScope ts = new TransactionScope())
{
if (feed.LastPostGuidNameChanged)
feed.Update();
if (postListLebSpor.Count > 0)
foreach (PostLebSpor post in postListLebSpor)
post.Add();
ts.Complete();
}
}
foreach (FeedWodSpor feed in feedListWodSpor)
{
// Go get the posts for each feed
List<PostWodSpor> postListWodSpor = PostWodSpor.GetNewRssPostsFromUrl(feed);
using (TransactionScope ts = new TransactionScope())
{
// if the Feed and its Guid changed, update the feed
if (feed.LastPostGuidNameChanged)
feed.Update();
// If it has posts, add them
if (postListWodSpor.Count > 0)
foreach (PostWodSpor post in postListWodSpor)
post.Add();
ts.Complete();
}
}
}
catch (Exception ex) { string x = ex.Message; throw; }
}
protected override void OnStart(string[] args)
{
feedGathererTimer.Enabled = true;
}
protected override void OnStop()
{
feedGathererTimer.Enabled = false;
}
}
}
feedMidSpor页面我获取列表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace newsService.Entities
{
[Serializable]
public class FeedMidSpor : BaseEntity
{
#region Fields
private string title;
private string url;
private string lastPostGuidName;
private bool lastPostGuidNameChanged = false;
#region Constructors
public FeedMidSpor()
{
}
public FeedMidSpor(int id, DateTime createdDate, string title, string url, string lastPostGuidName)
: base(id, createdDate)
{
this.title = title;
this.url = url;
this.lastPostGuidName = lastPostGuidName;
this.lastPostGuidNameChanged = false;
}
#endregion
#region Properties
public string Url
{
get { return url; }
set { url = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string LastPostGuidName
{
get { return lastPostGuidName; }
set
{
if (lastPostGuidName != value)
this.lastPostGuidNameChanged = true;
lastPostGuidName = value;
}
}
public bool LastPostGuidNameChanged
{
get { return lastPostGuidNameChanged; }
set { lastPostGuidNameChanged = value; }
}
#endregion
#region Methods
public void Update()
{
string connectionString1 = "Data Source=RAYYAN-THINK;Initial Catalog=newsProject;uid=rayyan1;password=rayyan@101;Connect Timeout=30";
string sql = "UPDATE FeedMidSporRss SET LastPostGuidName = @lastPostGuidName, Title = @title, Url = @url WHERE ID = @ID";
using (SqlConnection cn1 = new SqlConnection(connectionString1))
{
using (SqlCommand cmd = new SqlCommand(sql, cn1))
{
cn1.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@lastPostGuidName", this.lastPostGuidName);
cmd.Parameters.AddWithValue("@title", this.title);
cmd.Parameters.AddWithValue("@url", this.url);
cmd.Parameters.AddWithValue("@ID", this.Id);
cmd.ExecuteNonQuery();
cn1.Close();
}
// reset the flag
this.lastPostGuidNameChanged = false;
}
}
#endregion
#region Static Methods
public static List<FeedMidSpor> GetList()
{
List<FeedMidSpor> feedList1 = new List<FeedMidSpor>();
string connectionString = "Data Source=RAYYAN-THINK;Initial Catalog=newsProject;uid=rayyan1;password=rayyan@101;Connect Timeout=30";
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
string sql = "SELECT ID, Title, Url, LastPostGuidName, DateCreated FROM FeedMidSporRss ORDER BY Sequence";
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
while (rdr.Read())
{
feedList1.Add(new FeedMidSpor(Convert.ToInt32(rdr["Id"]), Convert.ToDateTime(rdr["DateCreated"].ToString()), rdr["Title"].ToString(), rdr["Url"].ToString(), rdr["LastPostGuidName"].ToString()));
}
}
catch (Exception ex) { string x = ex.Message; }
}
}
return feedList1;
}
#endregion
}
}
抽象baseintity类是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace newsService
{
public abstract class BaseEntity
{
#region Fields
private int id;
private DateTime createdDate;
#endregion
#region Constructors
protected BaseEntity()
{
}
protected BaseEntity(int id, DateTime createdDate)
{
this.id = (id != null) ? 0 : id;
this.createdDate = (createdDate != null) ? createdDate = DateTime.MinValue :createdDate;
}
#endregion
#region Properties
public DateTime CreatedDate
{
get { return createdDate; }
set { createdDate = value; }
}
public int Id
{
get { return id; }
set { id = value; }
}
#endregion
}
}
我使用的是Windows服务c#
答案 0 :(得分:0)
在构造函数中分配值之前,需要执行正确的检查:
此:
public FeedMidSpor(int id, DateTime createdDate, string title, string url, string lastPostGuidName)
: base(id, createdDate)
{
this.title = title;
this.url = url;
this.lastPostGuidName = lastPostGuidName;
this.lastPostGuidNameChanged = false;
}
应该是:
public FeedMidSpor(int id, DateTime createdDate, string title, string url, string lastPostGuidName)
: base(id, createdDate)
{
this.title = (String.IsNullOrEmpty(title))?"":title;
this.url = (String.IsNullOrEmpty(url))?"":url;
this.lastPostGuidName = (String.IsNullOrEmpty(lastPostGuidName))?"":lastPostGuidName ;
this.lastPostGuidNameChanged = false;
}
基类应如下所示:
protected BaseEntity(int id, DateTime createdDate)
{
this.id = (id != null) ? id : 0;
this.createdDate = (createdDate != null) ? createdDate : DateTime.MinValue ;
}
答案 1 :(得分:0)
我真的首先要感谢@Sudhakar和每个与我分享他的想法的人,但解决方案远非代码错误。最后在项目构建中出现错误,所以我清理项目,然后构建项目我卸载newsService,然后再安装它,我的项目运行完美!