以下MSDN教程 - Windows服务未安装

时间:2013-11-13 08:22:54

标签: c# .net service windows-services

所以在这里我再次体验到另一个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 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试

     

C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \ Projects \ WindowsService1 \ WindowsServ ice1 \ bin \ Debug> installutil   WindowsService1.exe Microsoft(R).NET Framework安装实用程序   版本4.0.30319.32559版权所有(C)Microsoft Corporation。所有   保留权利。

     

运行事务安装。

     

开始安装的安装阶段。看到的内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   总结的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio 2013 \ Projects \ Wind   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。   安装程序集' C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\窗口   sService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe&#39 ;.受影响   参数是: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 \ WindowsSe rvice1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe安装   service Service1 ...在日志中创建EventLog源Service1   应用...

     

安装阶段发生异常。   System.InvalidOperationException:无法打开服务控制管理器   在计算机上。'。此操作可能需要其他权限。该   抛出了内部异常System.ComponentModel.Win32Exception   以下错误消息:访问被拒绝。

     

安装的回滚阶段正在开始。看到内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   总结的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio 2013 \ Projects \ Wind   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。   回滚程序集' C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\风   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe&#39 ;.受影响   参数是: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 \ WindowsSe rvice1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe恢复事件   登录到源Service1的先前状态。

     

回滚阶段已成功完成。

     

已完成事务处理安装。安装失败了,而且   回滚已经完成。

     

C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \ Projects \ WindowsService1 \ WindowsServ ice1 \ bin \ Debug> installutil   WindowsService1 Microsoft(R).NET Framework安装实用程序   版本4.0.30319.32559版权所有(C)Microsoft Corporation。所有   保留权利。

     

初始化安装时发生异常:   System.IO.FileNotFoundException:无法加载文件或程序集   ' file:/// C:\使用rs \ Tommy \ Documents \ Visual Studio   2013 \项目\ WindowsService1 \ WindowsService1 \ b   在\调试\ WindowsService1'或其中一个依赖项。系统   找不到指定的文件..

     

C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \ Projects \ WindowsService1 \ WindowsServ ice1 \ bin \ Debug> installutil   WindowsService1.exe Microsoft(R).NET Framework安装实用程序   版本4.0.30319.32559版权所有(C)Microsoft Corporation。所有   保留权利。

     

运行事务安装。

     

开始安装的安装阶段。看到的内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   总结的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio 2013 \ Projects \ Wind   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。   安装程序集' C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\窗口   sService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe&#39 ;.受影响   参数是: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 \ WindowsSe rvice1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe安装   service Service1 ...在日志中创建EventLog源Service1   应用...

     

安装阶段发生异常。   System.InvalidOperationException:无法打开服务控制管理器   在计算机上。'。此操作可能需要其他权限。该   抛出了内部异常System.ComponentModel.Win32Exception   以下错误消息:访问被拒绝。

     

安装的回滚阶段正在开始。看到内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   总结的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio 2013 \ Projects \ Wind   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。   回滚程序集' C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\风   owsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe&#39 ;.受影响   参数是: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 \ WindowsSe rvice1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe恢复事件   登录到源Service1的先前状态。

     

回滚阶段已成功完成。

     

已完成事务处理安装。安装失败了,而且   回滚已经完成。

     

C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \ Projects \ WindowsService1 \ WindowsServ ice1 \ bin \ Debug>

还将INSTALLLOG文件输出到theDebug目录,其中包含(相同的信息):

  

运行事务安装。

     

开始安装的安装阶段。看到的内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   集会的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。

     

安装阶段发生异常。   System.InvalidOperationException:无法打开服务控制管理器   在电脑上'。此操作可能需要其他权限。该   抛出了内部异常System.ComponentModel.Win32Exception   以下错误消息:访问被拒绝。

     

安装的回滚阶段正在开始。看到内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   集会的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。

     

回滚阶段已成功完成。

     

已完成交易安装。

     

运行事务安装。

     

开始安装的安装阶段。看到的内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   集会的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。

     

安装阶段发生异常。   System.InvalidOperationException:无法打开服务控制管理器   在电脑上'。此操作可能需要其他权限。该   抛出了内部异常System.ComponentModel.Win32Exception   以下错误消息:访问被拒绝。

     

安装的回滚阶段正在开始。看到内容   C:\ Users \ Tommy \ Documents \ Visual Studio的日志文件   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.exe   集会的进步。该文件位于   C:\ Users \ Tommy \ Documents \ Visual Studio   2013 \项目\ WindowsService1 \ WindowsService1 \ BIN \调试\ WindowsService1.InstallLog。

     

回滚阶段已成功完成。

     

已完成交易安装。

然后我回到MSDN。要查看安装失败时该怎么做,而是我看到了:

If the service installs successfully, installutil.exe will report success.如果安装失败,则没有其他信息可以执行。


我已经花了好几天时间,并且我已经完成了8个教程,所有教程都有不完整或不好的代码(不起作用)。所以我最终决定再次信任MSDN,但像往常一样,我想知道到底发生了什么。

我的用户帐户具有完整的读/写/执行权限,是管理员,我已检查过计算机管理(&gt;服务和应用程序)并确认我的Windows服务列在那里。< / p>

我现在该怎么办?如何创建Windows服务(可行)?

2 个答案:

答案 0 :(得分:4)

An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on computer '.'. This operation might require other privileges.

以“管理员”身份运行您的开发人员命令提示符。作为管理员与以管理员身份运行流程不同。

来自MSDN:

  

在“开始”菜单或“开始页面”上,打开“开发人员命令提示符”的快捷菜单,然后选择“以管理员身份运行”。

答案 1 :(得分:1)

  

安装阶段发生异常。 System.InvalidOperationException:无法在计算机上打开服务控制管理器。&#39;。此操作可能需要其他权限。抛出了内部异常System.ComponentModel.Win32Exception,并显示以下错误消息:拒绝访问。

错误说明问题所在:您无权安装该服务。

如果计算机是域成员,则完全有可能本地管理员帐户没有安装服务所需的权限。

它也可能被AV软件阻止,或者您的服务需要添加到白名单中。可能性几乎无穷无尽,因此您需要找出具体错误是什么。

您可以从安全事件日志中获取更多信息。您也可以尝试使用sysinternals Process Monitor。它应该告诉你它想要做什么以及失败的是什么。

  

我的用户帐户具有完整的读/写/执行权限,是管理员

如果您使用的是域名,则可能需要成为域名管理员。