编译输出类型为 Exe 的 windows 服务
Compiling a windows service with an output type of Exe
目前,在开发 windows 服务时,我修改了 csproj 以在调试模式下将 OutputType 设置为 Exe,这样我就可以得到一个控制台 window 并且我可以轻松地调试服务。
我很好奇的是,这样在发布模式下投入生产有什么问题吗?我没有看到控制台 window,它似乎在通过 InstallUtil 安装服务然后启动时未显示或隐藏或未创建。
有什么想法吗?
大部分服务通常都是exe输出类型。是的,当它作为服务 运行 时不会有控制台,但只要您不从控制台读取任何内容,它就不会有问题。您可以将控制台作为服务写入,文本将被系统忽略。
通常我做的是让程序监视字符串 --debug
作为命令行参数传入,如果是,它会作为控制台应用程序启动服务,如果不是,它会启动它作为一项服务。以下是如何操作的示例:
static void Main(string[] args)
{
var debugMode = args.Contains("--debug", StringComparer.InvariantCultureIgnoreCase);
if (!debugMode)
{
ServiceBase[] servicesToRun =
{
new MyService();
};
ServiceBase.Run(servicesToRun);
}
else
{
var service = new MyService();
service.StartService(args);
Console.WriteLine("Service is now running, press enter to stop...");
Console.ReadLine();
service.StopService();
}
}
}
然后在我做的服务代码里面
public partial class MyService : ServiceBase
{
internal void StartService(string[] args)
{
OnStart(args);
}
internal void StopService()
{
OnStop();
}
//... The rest of the code here
}
目前,在开发 windows 服务时,我修改了 csproj 以在调试模式下将 OutputType 设置为 Exe,这样我就可以得到一个控制台 window 并且我可以轻松地调试服务。
我很好奇的是,这样在发布模式下投入生产有什么问题吗?我没有看到控制台 window,它似乎在通过 InstallUtil 安装服务然后启动时未显示或隐藏或未创建。
有什么想法吗?
大部分服务通常都是exe输出类型。是的,当它作为服务 运行 时不会有控制台,但只要您不从控制台读取任何内容,它就不会有问题。您可以将控制台作为服务写入,文本将被系统忽略。
通常我做的是让程序监视字符串 --debug
作为命令行参数传入,如果是,它会作为控制台应用程序启动服务,如果不是,它会启动它作为一项服务。以下是如何操作的示例:
static void Main(string[] args)
{
var debugMode = args.Contains("--debug", StringComparer.InvariantCultureIgnoreCase);
if (!debugMode)
{
ServiceBase[] servicesToRun =
{
new MyService();
};
ServiceBase.Run(servicesToRun);
}
else
{
var service = new MyService();
service.StartService(args);
Console.WriteLine("Service is now running, press enter to stop...");
Console.ReadLine();
service.StopService();
}
}
}
然后在我做的服务代码里面
public partial class MyService : ServiceBase
{
internal void StartService(string[] args)
{
OnStart(args);
}
internal void StopService()
{
OnStop();
}
//... The rest of the code here
}