我有一个ConsoleApp,但我想把它移到WinSvc应用程序库中。我考虑将soruce代码作为库项目移动,并在旧的Console to TEST中用于其他可能的开发目的,并在WinSvc中用于实际的WORK区域。因此,当我向基础库添加新内容时,它将导致出现在两个应用程序中。它将能够在ConsoleApp环境中进行测试,并且只需要为真实环境重建WinSvc部分(这是为了避免使用附加的调试模式)。
那么,你对这个模型有什么看法?或者这是一个好方法吗?或者您还有其他建议吗?
这可以是一个解决方案:
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();
}