2010-11-25 58 views
1

是否有工具或方法来测试我的Windows服务? 它工作与Visual Studio 2010编译罚款。测试新创建的Windows服务的工具?

我使用高级安装来创建一个安装包(MSI),但它无法启动!

欢呼声

+0

您如何尝试运行它? – 2010-11-25 13:25:15

回答

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

在应用程序中是否有任何日志记录?这可能是第一个检查解决方法的地方。用'工具'来测试'一些Windows服务'是相当困难的。如果您有更多来自日志记录的详细信息,并且无法弄清楚发生了什么问题,请告诉我们,以便我们能够提供帮助。

1

您不能直接运行Windows服务:您必须安装服务并启动它。

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

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); 
} 
1

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

System.Diagnostics.Debugger.Break()