我很困惑为什么我的Windows服务无法启动!我的服务没有开始工作,但是现在没有人工作 解决这个问题我的例外是“无法启动服务 在计算机上'。'。“和我的内部消息是” 服务没有及时响应启动或控制。“
我在windows中观察UAC(用户访问控制),我的visual studio运行为 admin和启动服务之前我在main中通过args安装Service 方法!
using System;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Windows.Forms;
namespace BotTelegram
{
static void Main(string [] args)
{
if (args.Contains("-i"))
{
System.Configuration.Install.AssemblyInstaller installer =
new System.Configuration.Install.AssemblyInstaller(Assembly.GetExecutingAssembly().Location, null)
{
UseNewContext = true
};
installer.Install(null);
installer.Commit(null);
if (!args.Contains("nomessage"))
System.Windows.Forms.MessageBox.Show(@"Installed Successfully!");
Console.WriteLine(@"Installed Successfully!");
return;
}
if (args.Contains("-u"))
{
System.Configuration.Install.AssemblyInstaller installer =
new System.Configuration.Install.AssemblyInstaller(Assembly.GetExecutingAssembly().Location, null)
{
UseNewContext = true
};
installer.Uninstall(null);
if (!args.Contains("nomessage"))
System.Windows.Forms.MessageBox.Show(@"Removed Successfully!");
Console.WriteLine(@"Removed Successfully!");
return;
}
if (args.Contains("-s1"))
{
var service = new ServiceController("PriceCheckerService", ".");
try
{
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
if (!args.Contains("nomessage"))
System.Windows.Forms.MessageBox.Show(@"Start Service!!");
return;
}
}
finally
{
service.Close();
}
try
{
if (service.Status == ServiceControllerStatus.Running)
{
if (service.CanStop)
{
service.Stop();
if (!args.Contains("nomessage"))
System.Windows.Forms.MessageBox.Show(@"Stopped Service!!");
return;
}
}
}
finally
{
service.Close();
}
}
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new PriceCheckerService()
};
ServiceBase.Run(ServicesToRun);()
};
ServiceBase.Run(ServicesToRun);
}
}