.Net windows services: install from referenced assembly
There are lots of examples how to install windows service in one line:
ManagedInstallClass.InstallHelper(
new[] { Assembly.GetExecutingAssembly().Location });
That works fine until service class is declared in exe module. But the same code doesn't work for me if service class is in referrenced assembly (not declared in executable, but in linked dll).
In such a case service is registered as well but can't be started as it is registered with dll path and points to dll ("service is not a win32 executable" message appears in event log when I try to start that)
If I change GetExecutingA开发者_开发问答ssembly().Location
to executable path, then no installers are found and service is not registered at all.
Is it possible to put service class into referenced assembly and still have ability to register service with minimum effort?
Thank you in advance!
here is some C# code that allows you to install/uninstall a service" manually" (without the need to declare custom RunInstaller attributes):
static void InstallService(string path, string name, string displayName, string description)
{
ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();
si.Parent = spi;
si.DisplayName = displayName;
si.Description = description;
si.ServiceName = name;
si.StartType = ServiceStartMode.Manual;
// update this if you want a different log
si.Context = new InstallContext("install.log", null);
si.Context.Parameters["assemblypath"] = path;
IDictionary stateSaver = new Hashtable();
si.Install(stateSaver);
}
static void UninstallService(string name)
{
ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();
si.Parent = spi;
si.ServiceName = name;
// update this if you want a different log
si.Context = new InstallContext("uninstall.log", null);
si.Uninstall(null);
}
精彩评论