I just tried this code, and it executes without any errors - but I don't see the service now in my Services window.
What could be the problem?
Thanks for all your help!
Shaul
On Tuesday, September 9, 2008 9:39:57 AM UTC+3, Marc Gravell wrote:
> You can deploy a service with an msi if you want. Personally, I find
> the simplest mechanism (for my needs) is to make the service self-
> installing through command-line switches (i.e. "-i" to install, "-u"
> to uninstall). I also tend to add a "-c" console mode (which runs the
> main code without bothering with the service layer) - this makes it
> easy to debug etc; or with no switches present it just runs the
> service like normal.
>
> You can use the regular installer code in your main code (as long as
> you run it as an admin), by adding code like below.
>
> Marc
>
> [RunInstaller(true)]
> public sealed class MyServiceInstallerProcess :
> ServiceProcessInstaller
> {
> public MyServiceInstallerProcess()
> {
> this.Account = ServiceAccount.NetworkService;
> }
> }
> [RunInstaller(true)]
> public sealed class MyServiceInstaller : ServiceInstaller
> {
> public MyServiceInstaller()
> {
> this.Description = "Service Description";
> this.DisplayName = "Service Name";
> this.ServiceName = "ServiceName";
> this.StartType =
> System.ServiceProcess.ServiceStartMode.Automatic;
> }
> }
>
> static void Install(bool undo, string[] args)
> {
> try
> {
> Console.WriteLine(undo ? "uninstalling" :
> "installing");
> using (AssemblyInstaller inst = new
> AssemblyInstaller(typeof(Program).Assembly, args))
> {
> IDictionary state = new Hashtable();
> inst.UseNewContext = true;
> try
> {
> if (undo)
> {
> inst.Uninstall(state);
> }
> else
> {
> inst.Install(state);
> inst.Commit(state);
> }
> }
> catch
> {
> try
> {
> inst.Rollback(state);
> }
> catch { }
> throw;
> }
> }
> }
> catch (Exception ex)
> {
> Console.Error.WriteLine(ex.Message);
> }
> }
> }