Windows 服务启动失败: 无法从命令行或调试器启动服务

嗨,我收到这个错误

无法从命令行或调试器启动服务。必须首先安装一个 Windows 服务(使用 installutil.exe) ,然后使用 ServerExplorer、 Windows 服务管理工具或 NET START 命令启动。

我不明白为什么我得到这个错误。 我的原则是:

{
string Hash = "";
string connectionstring = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionstring);
SqlCommand myCommand = new SqlCommand("GetNullHash", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
SqlDataReader rdr = myCommand.ExecuteReader();


while (rdr.Read())
{
string filename = @"\\" + rdr.GetString(3);
filename = System.IO.Path.Combine(filename, rdr.GetString(2));
filename = System.IO.Path.Combine(filename, rdr.GetString(1));
Hash = rdr.GetString(0);
Hash = computeHash(filename);


}
myConnection.Close();
return Hash;
}
229819 次浏览

您的代码与服务安装无关,这不是问题所在。

为了测试服务,您必须按指示安装它。

有关安装服务的详细信息,请参阅: 安装和卸载服务

手动安装服务

手动安装或卸载 Windows 服务(使用。NET Framework)使用实用程序 InstallUtil.exe。此工具可以在以下路径中找到(使用适当的框架版本号)。

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

安装

installutil yourproject.exe

卸载

installutil /u yourproject.exe

参见: 如何: 安装和卸载服务 (微软)

以编程方式安装服务

要使用 C # 以编程方式安装服务,请参见下面的类 ServiceInstaller (c-锐角)

我将建议创建一个安装项目的原因,而部署这似乎是最好的方便,没有头痛的手动复制文件。 遵循 Windows 服务设置创建教程并且您知道如何创建它。这个实例适用于 vb.net,但是对于任何类型都是一样的。

安装 Open CMD,并在 NET START {YourserviceName}中键入已安装的 {YourServiceName} -i类型,以启动您的服务

卸载

卸载打开 CMD 和键入 NET STOP {YourserviceName}一旦停止类型在 {YourServiceName} -u,它应该被卸载

观看 这个视频,我有同样的问题。他告诉你如何调试服务以及。

下面是他在 Visual Studio 2010/2012中使用基本的 C # Windows 服务模板的说明。

将其添加到 Service1.cs 文件:

public void onDebug()
{
OnStart(null);
}

如果处于 DEBUG 活动解决方案配置中,则更改 Main ()以这种方式调用服务。

static void Main()
{
#if DEBUG
//While debugging this section is used.
Service1 myService = new Service1();
myService.onDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);


#else
//In Release this section is used. This is the "normal" way.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}

请记住,尽管这是调试服务的一种非常棒的方法。它不调用 OnStop(),除非您显式地调用它,类似于我们在 onDebug()函数中调用 OnStart(null)的方式。

Goto App.config

找到

<setting name="RunAsWindowsService" serializeAs="String">
<value>True</value>
</setting>

设置为 False