will
will

Reputation: 897

How do I manually add an installer in a windows service?

I wrote an abstract class "PollingServiceBase" that inherits from ServiceBase in order to write windows service that had an override method Work that executes on a timer declared in the constructor. So I'm trying test it and I wrote a quick little class that writes a line including the datetime to a text file every minute. I need to install this service, but when i can't use the designer to "Add Installer" because PollingServiceBase is abstract. So can anyone either give me a workaround or point me to a resource that shows me how to do this manually?

Upvotes: 1

Views: 924

Answers (1)

Complexity
Complexity

Reputation: 5820

Just add a new one in your service by code:

ServiceInstaller installer = new ServiceInstaller();

And make sure that you use the properties correctly.

For an easier way, I recommend looking at the TopShelf. It's a nice library to write a Windows Service and make it runnable through F5. Basically, it's a console application.

Here's some information on TopShelf:

http://topshelf-project.com/

If you still want to stick with the basic implementation, here's some code:

using (TransactedInstaller installer = new TransactedInstaller())
{
    string path = string.Format("/assemblypath={0}",
                  System.Reflection.Assembly.GetExecutingAssembly().Location);
    string[] arguments = { path };
    InstallContext context = new InstallContext("", arguments);

    using (ProjectInstaller projectInstaller = new ProjectInstaller())
    {
        installer.Installers.Add(projectInstaller);
    }

    installer.Context = context;
    installer.Install(new Hashtable());
}

Upvotes: 1

Related Questions