【发布时间】:2013-11-25 18:10:38
【问题描述】:
所以在这里我又遇到了另一个 MSDN 失败。像往常一样,我决定学习如何用 C# 编写 Windows 服务,由于许多教程提供的代码不完整或写得不好,我决定使用 MSDN。
我已按照每一步操作,但我的服务无法安装。
我关注的页面是:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
我的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace WindowsService1
{
public partial class WindowsService1 : ServiceBase
{
public WindowsService1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}
}
}
我已经进入开发者命令提示符,cd 到包含我的 .exe 的目录(调试文件夹),我已经:
installutil WindowsService1.exe 也试过installutil.exe WindowsService1.exe 但安装总是失败,出现以下文字:
C:\Program Files (x86)\Microsoft Visual Studio 11.0>cd C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug>installutil WindowsService1.exe Microsoft (R) .NET Framework 安装实用程序 版本 4.0.30319.32559 版权所有 (C) Microsoft Corporation。全部 保留权利。
运行事务安装。
开始安装的安装阶段。看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 组装 mbly 的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog。 安装程序集 'C:\Users\Tommy\Documents\Visual Studio 2013\项目\窗口 sService1\WindowsService1\bin\Debug\WindowsService1.exe'。做作的 参数是:logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog 程序集路径 = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 安装 service Service1... 在日志中创建 EventLog 源 Service1 申请...
安装阶段发生异常。 System.InvalidOperationException:无法打开服务控制管理器 在计算机上 '.'。此操作可能需要其他权限。这 引发了内部异常 System.ComponentModel.Win32Exception 以下错误消息:访问被拒绝。
安装的回滚阶段即将开始。查看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 组装 mbly 的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog。 回滚程序集 'C:\Users\Tommy\Documents\Visual Studio 2013\项目\风 owsService1\WindowsService1\bin\Debug\WindowsService1.exe'。做作的 参数是:logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog 程序集路径 = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 恢复事件 记录到源 Service1 的先前状态。
回滚阶段成功完成。
事务安装已完成。安装失败,并且 已执行回滚。
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug>installutil WindowsService1 Microsoft (R) .NET Framework 安装实用程序 版本 4.0.30319.32559 版权所有 (C) Microsoft Corporation。全部 保留权利。
初始化安装时发生异常: System.IO.FileNotFoundException:无法加载文件或程序集 'file:///C:\Use rs\Tommy\Documents\Visual Studio 2013\项目\WindowsService1\WindowsService1\b in\Debug\WindowsService1' 或其依赖项之一。系统 找不到指定的文件..
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug>installutil WindowsService1.exe Microsoft (R) .NET Framework 安装实用程序 版本 4.0.30319.32559 版权所有 (C) Microsoft Corporation。全部 保留权利。
运行事务安装。
开始安装的安装阶段。看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 组装 mbly 的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog。 安装程序集 'C:\Users\Tommy\Documents\Visual Studio 2013\项目\窗口 sService1\WindowsService1\bin\Debug\WindowsService1.exe'。做作的 参数是:logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog 程序集路径 = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 安装 service Service1... 在日志中创建 EventLog 源 Service1 申请...
安装阶段发生异常。 System.InvalidOperationException:无法打开服务控制管理器 在计算机上 '.'。此操作可能需要其他权限。这 引发了内部异常 System.ComponentModel.Win32Exception 以下错误消息:访问被拒绝。
安装的回滚阶段即将开始。查看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 组装 mbly 的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog。 回滚程序集 'C:\Users\Tommy\Documents\Visual Studio 2013\项目\风 owsService1\WindowsService1\bin\Debug\WindowsService1.exe'。做作的 参数是:logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog 程序集路径 = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 恢复事件 记录到源 Service1 的先前状态。
回滚阶段成功完成。
事务安装已完成。安装失败,并且 已执行回滚。
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug>
它还将一个INSTALLLOG文件输出到Debug目录中,其中包含(相同的信息):
运行事务安装。
开始安装的安装阶段。看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 大会的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
安装阶段发生异常。 System.InvalidOperationException:无法打开服务控制管理器 在电脑上'.'。此操作可能需要其他权限。这 引发了内部异常 System.ComponentModel.Win32Exception 以下错误消息:访问被拒绝。
安装的回滚阶段即将开始。查看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 大会的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
回滚阶段成功完成。
事务安装已完成。
运行事务安装。
开始安装的安装阶段。看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 大会的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
安装阶段发生异常。 System.InvalidOperationException:无法打开服务控制管理器 在电脑上'.'。此操作可能需要其他权限。这 引发了内部异常 System.ComponentModel.Win32Exception 以下错误消息:访问被拒绝。
安装的回滚阶段即将开始。查看内容 C:\Users\Tommy\Documents\Visual Studio 的日志文件 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe 大会的进展。该文件位于 C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
回滚阶段成功完成。
事务安装已完成。
然后我回到 MSDN。要查看如果安装失败该怎么办,但我看到的是:
If the service installs successfully, installutil.exe will report success.没有其他信息说明如果安装失败该怎么办。
我在这方面花费了几天时间,并且已经学习了超过 8 个教程,所有这些教程都包含不完整或错误的代码(不起作用)。所以我最终决定再次信任 MSDN,但像往常一样,我想知道到底发生了什么。
我的用户帐户具有完整的读/写/执行权限,是管理员,我已检查计算机管理(> 服务和应用程序)并确认我的 Windows 服务没有列在其中。 p>
我现在该怎么办?如何创建 Windows 服务(有效)?
【问题讨论】:
-
如果您使用的是 windows vista 或更高版本,您应该使用“以管理员身份运行”运行命令提示符 - 即使您的帐户已经是管理员成员
-
您应该阅读有关 UAC 的内容并了解仅仅因为您在管理员组中并不意味着您的会话正在该级别运行。
-
请以管理员身份运行...以后尽量保持帖子简单,
short -
好的,所以删除了我推荐 topshelf 的答案,因为它(在我看来很奇怪)似乎是一个负面投票磁铁。尽管如此,作为托管 Windows 服务的一种广泛使用且通常很好的方式,值得一看:github.com/Topshelf/Topshelf
-
以管理员身份运行 devloper cmd,如果它解决了您的问题,请将答案标记为解决方案,以后尽量让您的帖子保持简单和简短
标签: c# .net service windows-services