gpt4 book ai didi

c# - Windows 服务 (c#) 未启动

转载 作者:可可西里 更新时间:2023-11-01 10:46:07 24 4
gpt4 key购买 nike

抱歉我的英语不好,我不是母语人士。

我正在尝试制作 Windows 服务。如果我尝试构建、安装和运行 VS 模板,我不会收到任何错误。

我已将我的 winform 应用程序移植到服务中,创建了一个安装程序,添加了一些数据源,添加了对 web 服务的引用,添加了一些类,但没有向 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()
{
...
}
...
...
...
}
}

最佳答案

在 Stack Trace 中,入口点是 Program.Main。从那里创建一个新的 Updater 并调用 getNameFiles。这将是开始的地方。

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

另一个技巧是,如果您的 Visual Studio 与您的服务位于同一台机器上,则在 Main(或 OnStart)中添加以下行:Debugger .启动()。这将告诉服务寻找 Visual Studio 进行调试 session 。更多信息请看这里:Debugger.Launch() on windows service in Windows 8

关于c# - Windows 服务 (c#) 未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com