2

I want to install a windows service programmatically by the example.

Here is the code snippet.

private static AssemblyInstaller GetInstaller()
{
    AssemblyInstaller installer = new AssemblyInstaller(
        typeof(YourServiceType).Assembly, null);
    installer.UseNewContext = true;
    return installer;
}

I don't know what is the "YourServiceType" here. The syntax of AssemblyInstaller Constructor in MSDN is

public AssemblyInstaller(
    Assembly assembly,
    string[] commandLine
)

UPDATE:

The wild thing is that I can't start the service if running the command "MyApplication.exe -install" under the folder bin\debug. However if in debug mode, which I put the argument in "Start Options" of the project property. It is okay. Why? I did following the steps at example. I set the "StartType" as "Automatic".

Community
  • 1
  • 1

1 Answers1

1

YourServiceType is the type name of your Windows service. If you've followed my directions from scratch, then you originally created your service using the template provided by Visual Studio. By default, this gives you a service class named something like Service1. If you have not changed the name of your class, use Service1. Otherwise, use the name you changed it to.

private static AssemblyInstaller GetInstaller()
{
    AssemblyInstaller installer = new AssemblyInstaller(
        typeof(Service1).Assembly, null);
    installer.UseNewContext = true;
    return installer;
}
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
  • It is not working, do I have to add an installer for the service in the project? –  Feb 20 '14 at 18:17
  • Yes, you need to follow the instructions here (specifically steps 6-8): http://stackoverflow.com/questions/593454/easiest-language-for-creating-a-windows-service/593803#593803 – Matt Davis Feb 20 '14 at 18:25
  • When you say you "can't start the service if running the command" from the debug folder, are you saying that it won't install? Or are you saying that it installs but then the service fails to start? If the former, make sure you're an administrator on the system. If the latter, try debugging the service by calling `System.Diagnostics.Debugger.Launch()` from `Main()`. My guess is that your service is crashing when not run from the debugger. – Matt Davis Feb 21 '14 at 00:33
  • It is the former, it won't install and I am the administrator. The other issue is when I install and start it in debug way,the service's performance is different with its behavior as a console application. I put the whole code and question at http://social.msdn.microsoft.com/Forums/vstudio/en-US/47c8ef47-ecfa-4dc2-a4aa-868b1f2607ea/programmatically-install-windows-service-failed?forum=netfxbcl#47c8ef47-ecfa-4dc2-a4aa-868b1f2607ea –  Feb 21 '14 at 01:20
  • What error are you getting when you try to install the service? – Matt Davis Feb 21 '14 at 06:19
  • The installLog records: No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Messaging\MyApplication\bin\Debug\MyApplication.exe assembly. Remove InstallState file because there are no installers. But I do have ProjectInstaller in my project. –  Feb 21 '14 at 14:04
  • If you look at the code of the ProjectInstaller.cs file, does the class have a [RunInstaller(true)] attribute on it? And is the class public? – Matt Davis Feb 21 '14 at 14:12