简体   繁体   English

Windows服务(C#)无法启动

[英]Windows service (c#) not starting

Apologies for my English, I'm not a native speaker. 抱歉,我不会说英语。

I'm trying to make a Windows service. 我正在尝试提供Windows服务。 If I try to build, install and run a VS template I don't get any errors. 如果我尝试构建,安装和运行VS模板,则不会出现任何错误。

I have ported my winform application to a service, made an installer, added some data sources, added a reference for the webservice, added some classes, but DIDN'T add any code to OnStart() and OnStop(). 我已经将Winform应用程序移植到服务,进行了安装,添加了一些数据源,添加了Web服务的引用,添加了一些类,但是DIDN并未向OnStart()和OnStop()添加任何代码。 My code builds correctly and I can start and stop the service from the service manager. 我的代码正确构建,可以从服务管理器启动和停止服务。

However, if I add some code to the service class (which I don't call anywhere) and if I don't add code to OnStart() and to OnStop() then I can't start the service and the error is something like "The service doesn't respond the control functions". 但是,如果我在服务类中添加了一些代码(我在任何地方都没有调用过),并且没有在OnStart()和OnStop()中添加代码,那么我将无法启动服务,并且错误是例如“服务不响应控制功能”。 In the event log I can see the exception: 在事件日志中,我可以看到异常:

  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()

And I can see here my function getNameFile() is throwing an Exception. 我可以在这里看到我的函数getNameFile()抛出异常。 However, this isn't called in my code because I have empty OnStart(). 但是,这在我的代码中没有被调用,因为我有空的OnStart()。 So, how can I find what went wrong if the event log doesn't write anything ( if it is in the OnStart() ) ? 因此,如果事件日志未写入任何内容(如果它在OnStart()中),我怎么能找到问题所在? And I can't attach a debugger to it because it throws this exception. 而且我无法为其附加调试器,因为它会引发此异常。

edit: Forgot to say, my code works correctly when I use windows forms but here I don't call anything in OnStart, the project builds without errors but I have an exception when starting the service. 编辑:忘了说了,当我使用Windows窗体时,我的代码可以正常工作,但是在这里我没有在OnStart中调用任何东西,项目的构建没有错误,但是启动服务时出现了异常。

EDIT 2: Program.cs code: 编辑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 code: 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();
        }
    }
}

EDIT 3: FIASMainClass.cs code: 编辑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()
                {
                ...
                }
    ...
    ...
    ...
    }
}

From you Stack Trace, the entry point is Program.Main . 在您的堆栈跟踪中,入口点是Program.Main From there a new Updater is created and the getNameFiles is called. 从那里创建一个新的Updater并调用getNameFiles That would be the place to start. 那将是一个起点。

As far as Debugging a windows service. 至于调试Windows服务。 You are right, this is indeed hard. 没错,这确实很难。 There are two tricks I know. 我知道有两个技巧。 The first one is in the Main (or OnStart) to set a Thread.Sleep before you do anything. 第一个是在Main(或OnStart)中设置Thread.Sleep然后再执行任何操作。 This way you have time to attach your debugger. 这样,您就有时间连接调试器。

The other trick, is if your Visual Studio is on the same machine as your service, in the Main (or OnStart ) add this line: Debugger.Launch() . 另一个技巧是,如果Visual Studio与服务位于同一台计算机上,则在Main (或OnStart )中添加以下行: Debugger.Launch() This will tell the service to seek out Visual Studio for a debugging session. 这将告诉服务寻找Visual Studio进行调试会话。 See here for more: Debugger.Launch() on windows service in Windows 8 有关更多信息,请参见此处: Windows 8中Windows服务上的Debugger.Launch()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM