C# 在C中调试窗口系统服务#
我收到错误“无法从命令行或调试器启动服务。必须首先安装Windows服务(使用installutil.exe),然后使用ServerExplorer、Windows服务管理工具或NET start命令启动 那么,有没有办法在不安装Windows服务的情况下运行或测试它?我是否应该在控制台应用程序中构建我的项目,然后在测试后将代码传输到Windows Server项目C# 在C中调试窗口系统服务#,c#,debugging,service,C#,Debugging,Service,我收到错误“无法从命令行或调试器启动服务。必须首先安装Windows服务(使用installutil.exe),然后使用ServerExplorer、Windows服务管理工具或NET start命令启动 那么,有没有办法在不安装Windows服务的情况下运行或测试它?我是否应该在控制台应用程序中构建我的项目,然后在测试后将代码传输到Windows Server项目 谢谢。我倾向于在我的服务类中添加一个静态Main方法,以便它可以作为控制台应用程序进行调试,但也可以作为服务安装和运行 类似于以下
谢谢。我倾向于在我的服务类中添加一个静态Main方法,以便它可以作为控制台应用程序进行调试,但也可以作为服务安装和运行 类似于以下内容:
public partial class ControllerService : ServiceBase
{
static void Main(string[] args)
{
ControllerService service = new ControllerService();
cmdLine = CommandLineParser.Parse(args);
if (Environment.UserInteractive)
{
switch (cmdLine.StartMode)
{
case StartMode.Install:
//Service Install Code Here
break;
case StartMode.Uninstall:
//Service Uninstall Code Here
break;
case StartMode.Console:
default:
service.OnStart(args);
consoleCloseEvent.WaitOne();
break;
}
}
else
{
ServiceBase.Run(service);
}
}
protected override void OnStart(string[] args)
{
//Do Start Stuff Here
}
protected override void OnStop()
{
if (Environment.UserInteractive)
{
consoleCloseEvent.Set();
}
}
}
您可以尝试将执行实际工作的代码保存在单独的程序集中,并从服务或控制台应用程序调用该代码进行测试。您可以使用两种方法
#if Debug
Debugger.Launch()
#endif
我希望这会有帮助。我想我必须尝试这种方法,它看起来比我现在使用的方法好多了using@gwhitake如何访问service.Onstart?由于其保护级别,它不可访问。@ClearLogic-service.Onstart()是可访问的,因为您是从服务类内部调用它的。