Windows服务 - 如果在共享库中,则启动代码不起作用

时间:2015-08-28 12:25:01

标签: c# windows-services

我创建了多个Windows服务,在每一个中我使用以下构造来维护我的服务或在测试时调试它。

将此代码提取到具有类dll的共享库WinServiceBase并调用WinServiceBase.StartOrInstallService(args, mainLogic, minutes)它无效。

服务似乎“启动”,因为它显示在Windows服务面板中,但没有任何反应,也没有记录。

服务

    public static void Main(string[] args)
    {
        IMainLogic mainLogic = new MainLogic();
        int minutes = 120;

        // WinServiceBase.StartOrInstallService(args, mainLogic, minutes);

        if (Environment.UserInteractive)
        {
            if (args.Length == 0)
            {
                mainLogic.Start();
                // timeout for debugging
                Thread.Sleep(TimeSpan.FromMinutes(minutes));

                mainLogic.Pause();
            }
            else if (args.Length == 1)
            {
                switch (args[0])
                {
                    case "-install":
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetCallingAssembly().Location });
                        break;
                    case "-uninstall":
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetCallingAssembly().Location });
                        break;
                }
            }
        }
        else
        {
            log.InfoFormat("Service: start...");
            ServiceBase.Run(WinServiceBase.GetInstance(mainLogic));
        }
    }

共享类库

ServiceBase只是IMainLogic接口的一个包装器,用于启动服务,从app.config读取其名称并提供基于上述主要方法的静态方法。

public class WinServiceBase : ServiceBase 
{
    public static StartOrInstallService(String[] args, IMainLogic mainLogic, int minutes)
    {
        // same code as above main
    }
}

0 个答案:

没有答案
相关问题