2016-03-04 152 views
-1

如果我作为控制台应用程序进行调试或运行,我的Windows服务将正常运行,但如果安装,它将显示在服务中,但不会运行。请帮助Windows服务无法正常运行c#

这是主要的功能

static void main(){ 

if (Environment.UserInteractive) 
      { 

       string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1"; 
       // Determine whether the directory exists. 
       if (!Directory.Exists(path)) 
       { 
        DirectoryInfo di = Directory.CreateDirectory(path); 
        //di.Delete(); 
       } 




       StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1\\program.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + " : Here if\n"); 
       //  sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : "); 
       // sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : "); 
       sw.Flush(); 
       sw.Close(); 

       Console.WriteLine("here1"); 
       Service1 service1 = new Service1(); 
       string[] args = { "Kun", "Singh" }; 
       service1.TestStartupAndStop(args); 
      } 
      else 
      { 
       string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1"; 
       // Determine whether the directory exists. 
       if (!Directory.Exists(path)) 
       { 
        DirectoryInfo di = Directory.CreateDirectory(path); 
        //di.Delete(); 
       } 




       StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Service1\\program.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + " : Here else\n"); 
      //  sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : "); 
      // sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : "); 
       sw.Flush(); 
       sw.Close(); 





       Console.WriteLine("here2"); 
       // Put the body of your old Main method here. 
       ServiceBase[] ServicesToRun; 
       ServicesToRun = new ServiceBase[] 
       { 
       new Service1() 
       }; 
       ServiceBase.Run(ServicesToRun); 


       // RunAsync().Wait(); 
       Service1 myServ = new Service1(); 
       myServ.write("hello bb"); 
       Console.Read(); 
      } 
+0

您确定该服务正在运行吗?转到运行并输入Services.msc,按回车键查看所有正在运行的服务。 –

+0

它的运行只有我甚至尝试重新启动....代码很好,大多在调试模式下,所有文件都被写入,并且DB工作...但是如果使用installutil.exe,它会安装但没有任何反应....我不' t什么样的问题,它早些时候运行在示例程序....是否有任何方式调试seeply –

+0

我甚至尝试过 [有条件的(“DEBUG_SERVICE”)] private static void DebugMode() { Debugger.Break(); }其工作在调试模式 –

回答

2

魔法门因为Environment.UserInteractive是返回false。您正在运行一项服务,这意味着没有用户界面。见Environment.UserInteractive Property

的UserInteractive财产报告虚假的Windows进程或 服务如IIS,没有用户界面中运行。如果此属性 为假,请不要显示模式对话框或消息框,因为 没有用于与用户交互的图形用户界面。

我建议尝试更改的条件,如果(!Environment.UserInteractive)

+0

我不知道,但我增加了新的WIX安装在相同的解决方案,现在一切正常,完美 –

0

我只是做了一个新的设置维克斯,现在它的工作,虽然有第二个问题...我有一些参考我Windows服务项目,我如何将它们包含在安装程序中,因为我在安装和运行时,我有说xxx.dll丢失的日志文件

我有以下参考 - > jose-jwt.dll,实体框架..

那么我该如何安装参考文件