windows - 安装 .net 2008 Windows 服务

标签 windows web-services visual-studio-2008 installutil

我刚刚创建了一个简单的测试 Windows 服务,但遇到了麻烦。我是 Windows 服务新手,所以我不知道我这样做是否正确。

namespace testWindowsService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {InitializeComponent();}

        protected override void OnStart(string[] args)
        {
            FileStream fs = new FileStream(@"c:\temp\started.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

            m_streamWriter.WriteLine("Service Started on \n" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }

        protected override void OnStop()
        {
            FileStream fs = new FileStream(@"c:\temp\stopped.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

            m_streamWriter.WriteLine("Service Stopped \n" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }
    }
}

然后我构建了该项目,并从“开始”->“所有程序”->“Microsoft Visual Studio 2008”->“Visual Studio 工具”->“Visual Studio 2008 命令提示符”打开命令提示符。根据我运行的提示:

installutil C:\Users\myUser\Documents\MyServices\testWindowsService\testWindowsService\bin\Debug\testWindowsService.exe

但我收到错误:

No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\myUser\Documents\MyServices\testWindowsService\testWindowsService\bin\Debug\testWindowsService.exe assembly.

我尝试用谷歌搜索它,但发现了很多死胡同和一半的答案。

谢谢

最佳答案

您需要创建一个安装程序。通读一下these articles看一个例子。特别是:

[RunInstallerAttribute(true)]
public class ProjectInstaller : Installer{
    private ServiceInstaller serviceInstaller1;
    private ServiceProcessInstaller processInstaller;

    public MyProjectInstaller(){
        // Instantiate installers for process and services.

        processInstaller = new ServiceProcessInstaller();
        serviceInstaller1 = new ServiceInstaller();

        // The services run under the system account.

        processInstaller.Account = ServiceAccount.LocalSystem;

        // The services are started manually.

        serviceInstaller1.StartType = ServiceStartMode.Manual;
        serviceInstaller2.StartType = ServiceStartMode.Manual;

        // ServiceName must equal those on ServiceBase derived classes.

        serviceInstaller1.ServiceName = "Hello-World Service 1";

        // Add installers to collection. Order is not important.

        Installers.Add(serviceInstaller1);
        Installers.Add(processInstaller);
    }
}

您可以非常轻松地将安装程序类添加到 VS2008 中的项目中,在添加新项目时它会显示为项目类型。

关于windows - 安装 .net 2008 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4380815/

相关文章:

php - 如何在 prestashop 网络服务中获取图像 ID

.net - 重复证书错误

algorithm - 搜索引擎如何合并倒排索引的结果?

android - 在 Windows 中,react-native 启动命令永远无法执行

web-services - 如何在 Tomcat Servlet 容器中验证和授权 webapp 请求

.net - LINQ-to-SQL 是否可以在插入时省略未指定的列,以便使用数据库默认值?

visual-studio-2008 - 我在哪里可以找到 VS2008 的 PowerCommands?

c++ - 发布运行时错误 VS2008 - 特定问题

java - 如何使用java模拟真实的鼠标点击?

excel - 满足条件(日期)时将各种事件合并到一个单元格中 google sheet