日期:2014-05-17  浏览次数:20467 次

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

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都试了一样的结果,

是什么原因导致的?!

------解决方案--------------------
你写一个console程序能调用这个dll并执行所有业务功能吗?
------解决方案--------------------
看看earlyWarningInfo类有没有引用其它第三方的控件,如COM组件
------解决方案--------------------
但是只要调用类似://FFWS.BLL.earlyWarningInfo eBll = new FFWS.BLL.earlyWarningInfo();

说明这里面的代码有问题啊。你得贴出这里的代码,或者自己加调试信息调试吧
------解决方案--------------------
提示:***的类型初始值设定项引发异常。

这很明显是你的代码问题了,不同的运行环境下,有些东西的获取方法是不同的,或者权限问题