Windows服务(c#)未启动

本文关键字:启动 服务 Windows | 更新日期: 2023-09-27 18:12:43

为我的英语道歉,我的母语不是英语。

我正在尝试做一个Windows服务。如果我尝试构建,安装和运行一个VS模板,我没有得到任何错误。

我已经将我的winform应用程序移植到一个服务,做了一个安装程序,添加了一些数据源,为webservice添加了一个引用,添加了一些类,但没有添加任何代码到OnStart()和OnStop()。我的代码构建正确,我可以从服务管理器启动和停止服务。

然而,如果我添加一些代码到服务类(我不调用任何地方),如果我不添加代码到OnStart()和OnStop(),那么我不能启动服务,错误是类似于"服务不响应控制函数"。在事件日志中,我可以看到异常:

  System.ArgumentOutOfRangeException
Stack:
 in System.String.InternalSubStringWithChecks(Int32, Int32, Boolean)
 in System.String.Substring(Int32, Int32)
 in UpdaterFIAS.FIASMainClass.getNameFile(System.String, System.String, System.String)
 in UpdaterFIAS.FIASMainClass..ctor()
 in UpdaterFIAS.Updater..ctor()
 in UpdaterFIAS.Program.Main()

我可以看到这里我的函数getNameFile()抛出了一个异常。然而,这并没有在我的代码中调用,因为我有空OnStart()。那么,如果事件日志不写任何东西(如果它是在OnStart()),我怎么能找到什么出错了?我不能给它附加调试器,因为它会抛出这个异常。

编辑:忘了说,我的代码工作正确,当我使用windows窗体,但这里我不调用任何在OnStart,项目构建没有错误,但我有一个异常,当启动服务。

编辑2:Program.cs代码:

namespace UpdaterFIAS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Updater() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

Updater.cs代码:

namespace UpdaterFIAS
{
    public partial class Updater : ServiceBase
    {
        public Updater()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }
        FIASMainClass mainFIAS = new FIASMainClass();
        protected override void OnStart(string[] args)
        {
            //timer1 = new System.Timers.Timer(5000);
            //timer1.Elapsed += timer1_Elapsed;
            //timer1.AutoReset = false;
            //timer1.Enabled = true;
            //ServiceStarterThread = new Thread(ServiceStarter);
            //ServiceStarterThread.Start();
            eventLog1.WriteEntry("In OnStart");
            //mainFIAS.Start();
        }
        protected override void OnStop()
        {
            //if (updater != null && (updater.ThreadState != System.Threading.ThreadState.Aborted && updater.ThreadState != System.Threading.ThreadState.Stopped)) updater.Abort();
            //if (log != null && (log.ThreadState != System.Threading.ThreadState.Aborted && log.ThreadState != System.Threading.ThreadState.Stopped)) log.Abort();
            //log.Abort();
            //timer1.Enabled = false;
            //timer1.Dispose();
            eventLog1.WriteEntry("In OnStop");
            //mainFIAS.Stop();
        }
    }
}

编辑3:FIASMainClass.cs代码:

        namespace UpdaterFIAS
        {
            class FIASMainClass
            {
                public FIASMainClass()
                { }
                public void Start()
                {
                    ServiceStarterThread = new Thread(ServiceStarter);
                    ServiceStarterThread.Start();
                }
                public void Stop()
                {
                    if (updater != null && (updater.ThreadState != System.Threading.ThreadState.Aborted && updater.ThreadState != System.Threading.ThreadState.Stopped)) updater.Abort();
                    if (log != null && (log.ThreadState != System.Threading.ThreadState.Aborted && log.ThreadState != System.Threading.ThreadState.Stopped)) log.Abort();
                }
                private void ServiceStarter()
                {
                ...
                }
    ...
    ...
    ...
    }
}

Windows服务(c#)未启动

从您的堆栈跟踪,入口点是Program.Main。从那里创建了一个新的Updater,并调用了getNameFiles。你可以从这里开始。

调试一个windows服务。你说得对,这确实很难。我知道两个窍门。第一个是在Main(或OnStart)中设置Thread.Sleep,然后再做任何事情。这样您就有时间附加调试器。

另一个技巧是,如果你的Visual Studio与你的服务在同一台机器上,在Main(或OnStart)中添加这一行:Debugger.Launch()。这将告诉服务寻找Visual Studio进行调试会话。在windows 8中