1

I'm trying to make a windows service using C#.

My problem is that I only have Visual Studio Express 2010 so I can't generate a "Service application". My console application is working and I installed it as a service using Inno Setup.

But of course, service is not starting. So my question is, what's the coding differences between a console application and a windows service - What I must do to make my application work as a service.

Thanks

Mathieu
  • 38
  • 7
  • 1
    Not quite a duplicate, but I think this will point you in the right direction: http://stackoverflow.com/q/7764088/56778 – Jim Mischel Jun 20 '13 at 21:53

2 Answers2

1

I would highly recommend looking at TopShelf to convert your console application to a Windows Service. The code changes required are really minimal; Essentially

public class Service
{
    public void Start() 
    {
         // your code when started
    }

    public void Stop() 
    {
         // your code when stopped
    }
}

public class Program
{
    public static void Main()
    {
        HostFactory.Run(x =>                                 
        {
            x.Service<Service>(s =>                    
            {
               s.ConstructUsing(name=> new Service());   
               s.WhenStarted(tc => tc.Start());            
               s.WhenStopped(tc => tc.Stop());           
            });
            x.RunAsLocalSystem();                          

            x.SetDescription("My service description");      
            x.SetDisplayName("ServiceName");                    
            x.SetServiceName("ServiceName");                   
        });                                                 
    }
}

Then to install it from the command line

service.exe install
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
0

We use something along these lines:

using System.ServiceProcess;
using System.Diagnostics;
using System;

namespace MyApplicationNamespace
{
    static class Program
    {
        static void Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {    
                switch (args[0])
                {
                    case "-debug":
                    case "-d":
                        StartConsole();
                        break;

                    default:
                        break;
                }
            }
            else
            {
                StartService();
            }    
        }

        private static void StartConsole()
        {
            MyApp myApp = new MyApp();
            myApp.StartProcessing();
            Console.ReadLine();
        }

        private static void StartService()
        {
            ServiceBase[] ServicesToRun;   
            ServicesToRun = new ServiceBase[] { new MyApp() };
            ServiceBase.Run(ServicesToRun);
        }               
    }
}

And MyApp would inherit from

System.ServiceProcess.ServiceBase

You can then install the service with

installutil app.exe

To run from console just use the -d or -debug switches.

jeremywho
  • 309
  • 5
  • 19