C# 安装自研Windows服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1580023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:00:58  来源:igfitidea点击:

Installing a self-developed Windows Service

c#windows-services

提问by Brandon Watson

I'm trying to deploy a service that I wrote. Here's the InstallLog file:

我正在尝试部署我编写的服务。这是安装日志文件:

Installing assembly 'c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.exe'.
Affected parameters are:
   logtoconsole = 
   assemblypath = c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.exe
   logfile = c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.InstallLog
Installing service TweetLinkService...
Creating EventLog source TweetLinkService in log Application...
Rolling back assembly 'c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.exe'.
Affected parameters are:
   logtoconsole = 
   assemblypath = c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.exe
   logfile = c:\Users\brwatson\Development\Projects\TweetLinks\TweetLinkQueue\bin\Debug\TweetLinkQueue.InstallLog
Restoring event log to previous state for source TweetLinkService.
An exception occurred during the Rollback phase of the System.Diagnostics.EventLogInstaller installer.
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
An exception occurred during the Rollback phase of the installation. This exception will be ignored and the rollback will continue. However, the machine might not fully revert to its initial state after the rollback is complete.

As you can see, it's not working. I am not sure how to proceed, and have hit the wall with Bing and Google. I have set the Account to LocalSystem for the serviceProcessInstaller1. The code compiles fine, but now I would like to run the thing...any ideas? I am an administrator on my box, and I am running the command:

如您所见,它不起作用。我不知道如何继续,并且与 Bing 和 Google 碰壁了。我已将 serviceProcessInstaller1 的帐户设置为 LocalSystem。代码编译得很好,但现在我想运行这个东西......有什么想法吗?我是我的机器的管理员,我正在运行以下命令:

InstallUtil TweetLinkQueue.exe

InstallUtil TweetLinkQueue.exe

from the VS2008 admin console.

从 VS2008 管理控制台。

UPDATED WITH /ShowCallStack option

使用 /ShowCallStack 选项更新

Call Stack

调用栈

An exception occurred during the Install phase.
System.Security.SecurityException: The source was not found, but some or all eve
nt logs could not be searched.  Inaccessible logs: Security.
   at System.Diagnostics.EventLog.FindSourceRegistration(String source, String m
achineName, Boolean readOnly)
   at System.Diagnostics.EventLog.SourceExists(String source, String machineName
)
   at System.Diagnostics.EventLogInstaller.Install(IDictionary stateSaver)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.ServiceProcess.ServiceInstaller.Install(IDictionary stateSaver)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.AssemblyInstaller.Install(IDictionary savedSt
ate)
   at System.Configuration.Install.Installer.Install(IDictionary stateSaver)
   at System.Configuration.Install.TransactedInstaller.Install(IDictionary saved
State)

and here is the constructor:

这是构造函数:

public TweetLinkService()
{
    InitializeComponent();

    if (!EventLog.SourceExists("TweetLinkQueue"))
    {
        EventLog.CreateEventSource("TweetLinkQueue", "Log");

        TweetLinksLog.Source = "TweetLinkQueue";
        TweetLinksLog.Log = "Log";

        TweetLinksLog.WriteEntry("Log Created!");
    }
}

UPDATED with ENtry Point:

使用入口点更新:

namespace TweetLinkQueue
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new TweetLinkService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

采纳答案by Matt Davis

I'm not sure what your specific problem is. It looks to me like the problem occurs while creating the EventLog source. Double-check that you've done that part correctly. You can reference the step-by-step here. EDIT: SPECIFICALLY LOOK AT STEP 9.The problem may be occuring because you're messing with the Application log instead of one specific to your application.

我不确定你的具体问题是什么。在我看来,问题是在创建 EventLog 源时发生的。仔细检查您是否正确完成了该部分。您可以在此处参考分步说明。 编辑:特别看第 9步。出现问题的原因可能是您正在处理应用程序日志,而不是特定于您的应用程序的日志。

There's nothing wrong with using InstallUtil, but if you need to install your service on a foreign machine, InstallUtil is not guaranteed to be there. You can follow this step-by-step to make your Windows service executable install/uninstall itself without the need of InstallUtil. See herefor those instructions.

使用 InstallUtil 没有任何问题,但是如果您需要在外国机器上安装您的服务,则不能保证 InstallUtil 存在。您可以按照此分步操作使您的 Windows 服务可执行文件自行安装/卸载,而无需 InstallUtil。有关这些说明,请参阅此处

回答by Jeff Sternal

The LocalSystem account doesn't normally have permission to read the Security event log (or to create event sources for that matter).

LocalSystem 帐户通常无权读取安全事件日志(或为此创建事件源)。

The easiest and safest solution is to create an event source installation program that you can run under your own administration-level credentials on any machine for which you'll want to run this. It might be even worth trying this as a simple test, just to see if your account has the permission to do this.

最简单和最安全的解决方案是创建一个事件源安装程序,您可以使用您自己的管理级别凭据在您想要运行它的任何机器上运行该程序。甚至可能值得将其作为一个简单的测试来尝试,以查看您的帐户是否有权执行此操作。

class Program {
    static void Main(string[] args) {
        EventLog.CreateEventSource("TestSource", "Application");
    }
}

If you execute that, does it succeed? You can check it by looking at the Application log's properties and browsing the Event sources on its Filter tab.

如果你执行它,它会成功吗?您可以通过查看应用程序日志的属性并浏览其过滤器选项卡上的事件源来检查它。

Alternately, since services have to be installed anyway, you could add an EventLogInstaller(which is misnamed - it's really an 'EventSourceInstaller' that will create EventLogsas needed) to the assembly instead of using EventLog.CreateEventSourcein the service's constructor.

或者,由于无论如何都必须安装服务,因此您可以向程序集中添加一个EventLogInstaller(名称错误 - 它实际上是一个将EventLogs根据需要创建的“EventSourceInstaller” ),而不是EventLog.CreateEventSource在服务的构造函数中使用。

回答by Tom B

I was just having this issue and it was because I wasn't running my visual studio command prompt as an administrator.

我只是遇到了这个问题,这是因为我没有以管理员身份运行我的 Visual Studio 命令提示符。

回答by Arifur Rahman

To solve this issue right click on your Visual Studio 2008 Command Prompt and click run as administrator then you run your command like installutil C:\mcWebService\bin\Debug\mcWebService.exe then it will show you successful message. Hope this will solve your solution.

要解决此问题,请右键单击 Visual Studio 2008 命令提示符并单击以管理员身份运行,然后运行 ​​installutil C:\mcWebService\bin\Debug\mcWebService.exe 等命令,然后它将显示成功消息。希望这将解决您的解决方案。

回答by CodingYoshi

My issue was a window was popping to enter credentials and I was entering my username without the domain. Once I entered domain\usernameall was fine.

我的问题是弹出一个窗口以输入凭据,而我输入的用户名没有域。一旦我输入域\用户名,一切都很好。

回答by hormberg

I got the same unexplainable errors when installing windows services. In our case the user was the problem.

我在安装 Windows 服务时遇到了同样无法解释的错误。在我们的例子中,用户是问题所在。

Installing the server with the administrator-user worked, but not for a local admin. This, however, is not a preferred solution, so we used this information to create a different user that could install the service.

使用管理员用户安装服务器有效,但不适用于本地管理员。但是,这不是首选解决方案,因此我们使用此信息创建了一个可以安装该服务的不同用户。