测试新创建的Windows服务的工具?

时间:2010-11-25 13:18:08

标签: c# visual-studio-2010 testing windows-services

是否有工具或方法来测试我的Windows服务? 它可以在Visual Studio 2010中正常编译。

我使用Advanced Installed来创建安装包(MSI),但它无法启动!

欢呼声

4 个答案:

答案 0 :(得分:4)

通过this answer查看lubos hasko以简化调试并简化服务安装(我已经取得了巨大的成功)。还建议调整log4net并使其以交互模式登录到控制台。

class TheService : ServiceBase
{
   static void Main(string[] args)
        {
            if (!Environment.UserInteractive)
            {
                Run(new TheService());
            }
            else
            {
                // If interactive, start up as a console app for easy debugging and/or installing/uninstalling the service
                switch (string.Concat(args))
                {
                    case "/i":
                        ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "/u":
                        ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                    default:
                        Console.WriteLine("Running service in console debug mode (use /i or /u to install or uninstall the service)");
                        var service = new TheService();
                        service.OnStart(null);
                        Thread.Sleep(Timeout.Infinite);
                        break;
                }
            }
        }
   }

答案 1 :(得分:1)

您的申请中是否有任何记录?这可能是第一个检查修复方法的地方。使用“工具”测试“某些Windows服务”非常困难。如果您从日志记录中获得了更多详细信息,并且无法弄清楚出了什么问题,请告知它,以便我们能够提供帮助。

答案 2 :(得分:1)

您无法直接运行Windows服务:您必须安装该服务并启动它。

由于在开发服务时安装服务往往不方便,因此值得修改服务的引导代码,以检测它是作为服务运行还是以交互方式运行,在后一种情况下,显示Windows表单。< / p>

ServiceBase service = ...;

if (Environment.UserInteractive)
{
    // run as application
    Application.EnableVisualStyles();
    Application.Run(new SomeForm()); // the form should call OnStart on the service
}
else
{
    // run as service
    ServiceBase.Run(service);
}

答案 3 :(得分:1)

我假设您正在使用安装程序类来创建和安装服务(派生自System.Configuration.Install.Installer)?如果是这样,请将此行代码放在安装程序的ctr或OnBeforeInstall覆盖中。然后,您可以附加调试器,并调试安装过程:

System.Diagnostics.Debugger.Break()
相关问题