windows服务在windows server 2008上运行的问题
开发了一个windows服务,在本地环境(win7)运行测试可以用,但是在目标机上(windows server 2008)上就无法运行,服务的OnStart()方法执行结束后,不执行Timer组件的Elapsed事件处理程序。我做了自定义事件日志输出,明明本地运行的时候可以输出更多的日志,而服务器环境上就只输出到OnStart()方法里的日志,往后都不运行了。
第一次写windows服务,测试什么的太捉鸡了,尤其在目标机器上,根本没法测呀。求大神解决。
代码如下:
public partial class XmlToSqlService : ServiceBase { private ImportTimer m_timer; private EventLog m_logger = new EventLog(); //private EventLogger m_logger; public XmlToSqlService() { InitializeComponent(); this.AutoLog = false; // create an event source, specifying the name of a log that // does not currently exist to create a new, custom log if (!EventLog.SourceExists("MySource")) { EventLog.CreateEventSource("MySource", "MyXmlLog"); } // configure the event log instance to use this source name m_logger.Source = "MySource"; m_logger.WriteEntry("XmlToSqlService is initializing..."); m_timer = new ImportTimer(m_logger); m_logger.WriteEntry("XmlToSqlService finished initializing"); } protected override void OnStart(string[] args) { m_timer.CreateTimer(); m_logger.WriteEntry("XmlToSqlService Started"); } protected override void OnStop() { m_timer.Stop(); m_logger.WriteEntry("XmlToSqlService Stoped!"); } protected override void OnPause() { m_timer.Stop(); m_logger.WriteEntry("XmlToSqlService Paused!"); } protected override void OnContinue() { m_timer.CreateTimer(); m_logger.WriteEntry("XmlToSqlService Continued!"); } } public class ImportTimer { private const string DIRECTORY = @"D:\thyb"; //存放xml文件的路径 F:\thyb private const string DIRECTORY_FASLE = @"D:\thyb_error"; //存放数据有错误的xml文件 private const string EXTEND = ".xml"; //后缀名为xml private const int MINT1 = 10; //第一次导入的分钟 private const int MINT2 = 37; //第二次导入的分钟 private const int MINT3 = 23; //计算小时API写入文件的操作 private Timer m_timer; public EventLog m_logger; public ImportTimer(EventLog logger) { m_logger = logger; CreateDirectory(); m_logger.WriteEntry("ImportTimer Contructed!"); } public void Stop() { m_timer.Dispose(); } public void Dispose() { m_timer.Dispose(); } private void CreateDirectory() { //若不存在存放错误信息的xml文件,则创建 if (!Directory.Exists(DIRECTORY_FASLE)) { Directory.CreateDirectory(DIRECTORY_FASLE); } } public void CreateTimer() { m_timer = new Timer(timer_Elapsed, this, 0, 60 * 1000); } private void timer_Elapsed(object sender) { m_logger.WriteEntry("Timer Begin One Elapsed"); import(); } private void import() //执行将所有xml文件导入数据库 { m_logger.WriteEntry("step into import()"); var files = GetFileList(DIRECTORY); m_logger.WriteEntry("step import(),file count=" + files.Count()); foreach (var file in files) { try { else { var import = new ImportToSql(); var success = import.xmltodb(file); if (success == 1) { File.Delete(file); //导入成功后删掉xml文件 m_logger.WriteEntry(file + " xml import successfully"); } if (success == 0) { int index = file.LastIndexOf(@"\"); if (!File.Exists(DIRECTORY_FASLE + file.Substring(index))) File.Move(file, DIRECTORY_FASLE + file.Substring(index)); m_logger.WriteEntry(file + " xml move to error directory"); } } } catch (Exception ee) { m_logger.WriteEntry("Exception:" + file.Substring(index) + ":" + ee.Message;) } } } //获得dir文件夹下所有文件名 private IEnumerable<string> GetFileList(string dir) { var directoryPath = dir; var files = Directory.GetFiles(directoryPath);//获取目录下的所有文件 var xmlFiles = new List<string>(); foreach (var file in files) { //过滤为空的xml文件 using (var fs = new FileStream(file, FileMode.Open)) { using (var sr = new StreamReader(fs)) { if (sr.ReadToEnd() == string.Empty && file.EndsWith(EXTEND)) { sr.Close(); File.Delete(file); continue; } } } //过滤后缀名为xml的文件 if (file.EndsWith(EXTEND)) { xmlFiles.Add(file); } } string[] directories = Directory.GetDirectories(directoryPath); if (directories.Count() != 0) { foreach (var directy in directories) { var xmlDirecFiles = GetFileList(directy); xmlFiles.AddRange(xmlDirecFiles); } } return xmlFiles; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论