【问题标题】:WinSvc and Its Console version to TestWinSvc 及其要测试的控制台版本
【发布时间】:2019-02-13 13:23:41
【问题描述】:
我有一个 ConsoleApp,但我想将它移到 WinSvc 应用程序库中。我考虑将源代码移动为 一个库项目,并在 old Console to TEST for other possible development 和 WinSvc 中使用它真正的工作区。因此,当我向 基础库 添加新内容时,这将导致出现在两个应用程序中。并且可以在ConsoleApp环境中进行我的测试,真实环境只需要重建WinSvc部分(避免使用附加调试模式)。
那么,您对这个模型有什么看法?或者这是一个好方法吗?或者您有什么其他建议?
【问题讨论】:
标签:
asp.net
dll
windows-services
console-application
【解决方案1】:
这可能是一个解决方案:
public static void Main(string[] args)
{
MyWinSvcClass mySvc = new MyWinSvcClass();
if (Environment.UserInteractive)
{
// If the executable is started on console
mySvc.RunAsConsole(args);
}
else
{
// If the executable is started as a service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { mySvc };
ServiceBase.Run(ServicesToRun);
}
}
在您的服务类中定义 RunAsConsole():
public void RunAsConsole(string[] args)
{
Log("Service is started on console:");
OnStart(args);
Log("Service session is ended.");
OnStop();
}