I changed my console application to use as windows service using ServiceBase. I installed it using the following command. But I didn't find the service in services. I checked the log it says
"No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\\Test\\MyService.exe assembly"
How do I create installer for Console application? Please let me know.
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\MyService.exe"
using System.ServiceProcess;
public static class Program
{
public static bool Cancelled { get; set; }
#region Nested classes to support running as service
public const string ServiceName = "MyService";
public class Service : ServiceBase
{
public Service()
{
ServiceName = Program.ServiceName;
}
protected override void OnStart(string[] args)
{
Program.Start(args);
}
protected override void OnStop()
{
Program.Stop();
}
}
#endregion
static void Main(string[] args)
{
if (!Environment.UserInteractive)
// running as service
using (var service = new Service())
ServiceBase.Run(service);
else
{
// running as console app
Start(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Stop();
}
}
private static void Start(string[] args)
{
// onstart code here
try
{
SaveMessage();
}
catch (Exception e)
{
LogError();
}
}
private static void Stop()
{
// onstop code here
DisposeAll();
}
}
I believe you need to extend from the System.Configuration.Install.Installer
Something like
public class ServiceRegister: Installer
{
public ServiceRegister()
{
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
#if RUNUNDERSYSTEM
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
#else
// should prompt for user on install
processInstaller.Account = ServiceAccount.User;
processInstaller.Username = null;
processInstaller.Password = null;
#endif
serviceInstaller.DisplayName = "SomeName";
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "SomeName";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
My favourite way to install a service is to use the SC
command line utilities.
Full syntax (to scare everybody !)
sc [<ServerName>] create [<ServiceName>] [type= {own | share | kernel | filesys | rec | interact type= {own | share}}] [start= {boot | system | auto | demand | disabled}] [error= {normal | severe | critical | ignore}] [binpath= <BinaryPathName>] [group= <LoadOrderGroup>] [tag= {yes | no}] [depend= <dependencies>] [obj= {<AccountName> | <ObjectName>}] [displayname= <DisplayName>] [password= <Password>]
In simple terms,
SC create YourServiceName start= auto binPath= "path/to/your/exe" DisplayName= "Your Display Name"
To remove a service, the command is
SC delete YourServiceName
The above commands need to run from a command prompt with Admin rights. Please note the space after the "=" sign is important.
Related SO post
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.