安装 .net 2008 Windows 服务

发布于 2024-10-06 15:43:53 字数 1857 浏览 0 评论 0原文

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

然后我构建了项目并从“开始”->“打开命令提示符”所有程序 ->微软 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.

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

谢谢

I just created a simple test Windows Service and am having trouble. I'm new to Windows Services so I don't know if I'm even doing this right.

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

Then I built the project and opened Command Prompt from Start -> All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt. From the prompt I ran:

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

But I get the error:

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.

I've tried googling it but found a lot of dead ends and half answers.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-10-13 15:43:53

您需要创建一个安装程序。通读这些 文章 查看示例。特别是:

[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 中的项目中,在添加新项目时它会显示为项目类型。

You need a create an installer. Have a read through these articles to see an example. In particular:

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

You can quite easily add an installer class to your project in VS2008, it appears as an item type when adding a new item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文