【问题标题】:Self install Windows Service in .NET在 .NET 中自行安装 Windows 服务
【发布时间】:2011-05-07 20:08:25
【问题描述】:

我已阅读this question。我有同样的问题,但我不明白 lubos hasko 的答案。我该怎么做?有人能发给我完整的演练吗?

当我运行下面的代码时,安装了一些东西,但在服务列表中,我找不到它。

我有这个,但这不起作用:

using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{

public class Service1 : ServiceBase
{
    public Service1()
    {
        File.AppendAllText("sss.txt", "ccccc");
    }

    protected override void OnStart(string[] args)
    {
        File.AppendAllText("sss.txt", "asdfasdf");
    }

    protected override void OnStop()
    {
        File.AppendAllText("sss.txt", "bbbbb");
    }


    static void Main(string[] args)
    {
        if (System.Environment.UserInteractive)
        {
            string parameter = string.Concat(args);
            switch (parameter)
            {
                case "--install":
                    ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                    break;
                case "--uninstall":
                    ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                    break;
            }
        }
        else
        {
            ServiceBase.Run(new Service1());
        }


        Console.ReadKey();
    }
 }
}

我也不明白这个:

if (System.Environment.UserInteractive) ...

【问题讨论】:

    标签: c# windows-services


    【解决方案1】:

    System.Environment.UserInteractive 属性告诉你 无论是 Windows 进程还是在没有用户界面的情况下运行的 IIS 等服务。

    如果此属性为 false,则不要显示模式对话框或消息框,因为没有供用户交互的图形用户界面。 Source

    也请查看this 文章。

    【讨论】:

    • 谢谢,那篇文章对我很有帮助。但在文章中,作者使用了 installutil。我不想使用 installutil。有什么选择吗?答案在这篇文章stackoverflow.com/questions/1449994/…,但我不知道怎么用。
    • 您为什么不想使用 installutil 安装服务?如果是因为权限,您将无法在没有管理员权限的情况下安装服务。没有办法解决这个问题。
    • 其实我还在用installutil,但是通过ManagedInstallerClass.InstallHelper。这才是重点。当我部署我的程序时,我也不需要部署 installutil.exe。安装 WS 是在使用 Inno Setup 的应用程序安装期间进行的,并且这是在管理员权限下进行的,所以没问题...
    【解决方案2】:

    首先,在您的 Service1 构造函数中设置 ServiceName 属性。

    摘自MSDN

    对于从 ServiceBase 继承的类,您需要在构造函数中实现的最低限度是在组件上设置 ServiceName。构造函数中没有特别需要其他处理。您应该在 OnStart 中而不是在构造函数中处理大多数初始化。

    其次,从命令行运行服务时,您需要将参数传递给服务。 --install 用于安装,--uninstall 用于卸载 - 查看您的 switch 语句,它在输入参数上执行此操作。

    【讨论】:

    • 顺便说一句,当向项目添加新项目时,您可以选择 Windows 服务(右键单击项目,在弹出菜单中选择添加 -> 新项目...并找到 Windows 服务那里)。像这样添加后,您可以很好地设置服务的属性并添加安装程序等。
    • 您可以随时查看背后的代码,了解它是如何实现的。 (代码不会是我听到的最漂亮的,但同样,这是一个开始)
    【解决方案3】:

    这是我的完整解决方案,并且有效。与this问题的答案基本相同。

    using System;
    using System.Configuration.Install;
    using System.Reflection;
    using System.ServiceProcess;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program : ServiceBase
        {
            static void Main(string[] args)
            {
    
                AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
    
    
                if (System.Environment.UserInteractive)
                {
                    string parameter = string.Concat(args);
                    switch (parameter)
                    {
                        case "--install":
                            ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                            break;
                        case "--uninstall":
                            ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                            break;
                    }
                }
                else
                {
                    ServiceBase.Run(new Program());
                }
    
    
    
            }
    
            private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
            }
    
            public Program()
            {
                this.ServiceName = "My Service";
                File.AppendAllText(@"C:\Temp\sss.txt", "aaa");
    
            }
    
            protected override void OnStart(string[] args)
            {
                base.OnStart(args);
    
                File.AppendAllText(@"C:\Temp\sss.txt", "bbb");
            }
    
            protected override void OnStop()
            {
                base.OnStop();
    
                File.AppendAllText(@"C:\Temp\sss.txt", "ccc");
            }
        }
    }
    

    在同一个项目中创建这个类:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        [RunInstaller(true)]
        public class MyWindowsServiceInstaller : Installer
        {
            public MyWindowsServiceInstaller()
            {
                var processInstaller = new ServiceProcessInstaller();
                var serviceInstaller = new ServiceInstaller();
    
                //set the privileges
                processInstaller.Account = ServiceAccount.LocalSystem;
    
                serviceInstaller.DisplayName = "My Service";
                serviceInstaller.StartType = ServiceStartMode.Automatic;
    
                //must be the same as what was set in Program's constructor
                serviceInstaller.ServiceName = "My Service";
                this.Installers.Add(processInstaller);
                this.Installers.Add(serviceInstaller);
            }
        }
    }
    

    使用参数--install/--uninstall 在 Windows 7 上以管理员身份运行此程序。检查 temp 中的错误日志。检查同一路径上的工作日志。

    【讨论】:

    • 这在 VB 中为我解决了,使用 C# 到 VB 转换器,我必须添加对 System.Configuration.Install 和 System.ServiceProcess 的引用
    猜你喜欢
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多