开发者

Installing a .net 2008 windows service

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\Deb开发者_Python百科ug\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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜