【求助帖】Windows服务调用外部dll异常

net_xiaojian 2012-09-02 06:40:41

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.IO;

namespace WSForXMLReport
{
public partial class ServiceXML : ServiceBase
{
System.Timers.Timer timer = null;

public ServiceXML()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1000 * 5; //5s

timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}

protected override void OnStop()
{
timer.Enabled = false;

//页面中使用:System.Web.HttpContext.Current.Server.MapPath
string strPath = AppDomain.CurrentDomain.BaseDirectory;

string path = strPath + @"/ErrStop.txt";

//文件不存在就创建然后读写该文件,会提示文件被占用。

if (!File.Exists(path))
{
FileStream fs = File.Create(path);//若文件不存在就先创建
fs.Close();
}

StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine(DateTime.Now + ":stop ok");
sw.Flush();
sw.Close();

}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.timer.Enabled = false;

#region 定时执行程序

//在这里编写需要定时执行的逻辑代码
//FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();
//List<FFWS.Model.earlyWarningInfo> lst = eBll.GetStcdRealDataReport();

string str = "abc";


//FFWS.Web.WebReference.IReportReceiveService service = new FFWS.Web.WebReference.IReportReceiveService();


//Global文件只能使用下面的方法查找目录,可能是应用程序还未启动。
//页面中使用:System.Web.HttpContext.Current.Server.MapPath
string strPath = AppDomain.CurrentDomain.BaseDirectory;

string path = strPath + @"/Err.txt";

//文件不存在就创建然后读写该文件,会提示文件被占用。

if (!File.Exists(path))
{
FileStream fs = File.Create(path);//若文件不存在就先创建
fs.Close();
}

StreamWriter sw = new StreamWriter(path, true);
sw.WriteLine(DateTime.Now + " " + str + ": start");
sw.Flush();
sw.Close();


#endregion

this.timer.Enabled = true;
}
}
}



//首先windows服务这个项目也添加了相关的引用
但是只要调用类似://FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();
下面的代码都不执行,注释掉了,运行就正常,vs2008/vs2010都试了一样的结果,

是什么原因导致的?!
...全文
318 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-09-06
  • 打赏
  • 举报
回复
提示:***的类型初始值设定项引发异常。

这很明显是你的代码问题了,不同的运行环境下,有些东西的获取方法是不同的,或者权限问题
孟子E章 2012-09-06
  • 打赏
  • 举报
回复
但是只要调用类似://FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();

说明这里面的代码有问题啊。你得贴出这里的代码,或者自己加调试信息调试吧
SinGooCMS 2012-09-03
  • 打赏
  • 举报
回复
FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();
---
这个至少运行了构造方法.
net_xiaojian 2012-09-02
  • 打赏
  • 举报
回复
新问题又来了
我创建的windows service和其他项目都在同一个解决方案下,我本地测试通过,每小时都准时上报,可是放在服务器上,全不能用,还有哪些问题需要注意???
yjicdgv1987 2012-09-02
  • 打赏
  • 举报
回复
看看earlyWarningInfo类有没有引用其它第三方的控件,如COM组件
net_xiaojian 2012-09-02
  • 打赏
  • 举报
回复
提示:***的类型初始值设定项引发异常。
net_xiaojian 2012-09-02
  • 打赏
  • 举报
回复
to 1s
我只是一句简单的声明而已:
//FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();

它就不执行,注释掉了就往下执行并写txt文件。
net_xiaojian 2012-09-02
  • 打赏
  • 举报
回复
我试试!
  • 打赏
  • 举报
回复
你写一个console程序能调用这个dll并执行所有业务功能吗?

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧