Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中自行安装Windows服务_C#_Windows Services - Fatal编程技术网

C# 在.NET中自行安装Windows服务

C# 在.NET中自行安装Windows服务,c#,windows-services,C#,Windows Services,我读过。我也有同样的问题,但我不明白卢布斯·哈斯科的答案。我该怎么做呢?有人能给我发完整的演练吗 当我运行下面的代码时,安装了一些东西,但在服务列表中,我找不到它 我有这个,但这个不行: using System; using System.Collections.Generic; using System.Configuration.Install; using System.Linq; using System.Reflection; using System.ServiceProcess;

我读过。我也有同样的问题,但我不明白卢布斯·哈斯科的答案。我该怎么做呢?有人能给我发完整的演练吗

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

我有这个,但这个不行:

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) ...

System.Environment.UserInteractive
属性告诉您 无论是Windows进程还是像IIS这样运行时没有用户界面的服务

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


也请检查文章。

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

摘录自:

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


第二,当从命令行运行服务时,需要向服务传递参数
--install
用于安装,
--uninstall
用于卸载-查看您的switch语句,它是在输入参数上执行的。

这是我的完整解决方案,它可以工作。这基本上与问题的答案相同

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);
        }
    }
}

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

谢谢,那篇文章确实帮助了我。但在本文中,作者使用了installutil。我不想使用installutil。有什么选择吗?答案在本文中,但我不知道如何使用它。为什么您不想使用installutil安装服务?如果是因为权限问题,您将无法安装没有管理员权限的服务。实际上,我仍然在使用installutil,但是通过ManagedInstallerClass.InstallHelper。这就是重点。当我部署我的程序时,我不需要也部署installutil.exe。安装WS是在应用程序安装期间使用Inno安装程序进行的,这是在管理员权限下进行的,因此没有问题…顺便说一句,在向项目添加新项目时,您可以选择Windows服务(右键单击项目,在弹出菜单中选择添加->新建项目…并在那里找到Windows服务)。在像这样添加之后,您可以很好地设置服务的属性并添加安装程序等。您可以随时查看后面的代码以了解它是如何实现的。(我听到的代码不会是最漂亮的,但再次强调,这是一个开始)我在VB中使用C#to VB转换器实现了这一点,我必须添加对System.Configuration.Install和System.ServiceProcess的引用