问题
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".
回答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;
}
来源:https://stackoverflow.com/questions/21912515/servicetype-in-assemblyinstaller