【问题标题】:Installing a .net 2008 windows service安装 .net 2008 windows 服务
【发布时间】:2011-05-21 19:36:04
【问题描述】:

我刚刚创建了一个简单的测试 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.

我试过谷歌搜索,但发现很多死胡同和一半答案。

谢谢

【问题讨论】:

  • 您不需要记录服务的启动和停止,这将为您完成,并且条目将出现在事件日志中。
  • 如果它解决了您的问题,您是否有机会接受答案?

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


【解决方案1】:

您需要创建一个安装程序。阅读 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 中为您的项目添加一个安装程序类,它在添加新项目时显示为项目类型。

【讨论】:

  • 和 System.Configuration.Install 一样?
  • 是的。我发布的这两个链接都在其内容中指明了System.Configuration.Install
  • 抱歉,在评论之前我没有看到链接……想删除评论。
猜你喜欢
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多