9

i have a simple c# application that needs to run as service. how do i make it run as a service instead of just as an executable?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • possible duplicate of [resources on creating a windows service using c#](http://stackoverflow.com/questions/1688382/resources-on-creating-a-windows-service-using-c) – Joe Jul 12 '10 at 20:19
  • Here I have found step-by-step instructions: https://stackoverflow.com/a/593803/7713750 – Rekshino Oct 10 '17 at 07:47

4 Answers4

3

There is a tempate called "Windows Service" in visual studio. If you have any questions let me know, I write services all day long.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
3

Visual C# 2010 Recipies has an example in it which will show you exactly how to do this, which I've tried using VS 2008 and .NET 3.5.

It amounts to this:

  1. Create a new "Windows Service" application in Visual Studio
  2. Port your application's source into the service's execution model, AKA your Main function becomes part of an event handler triggered by a timer object or something along those lines
  3. Add a Service Installer class to your Windows Service project - it'll look something like this code snippet below:

    [RunInstaller(true)]
    public partial class PollingServiceInstaller : Installer
    {
        public PollingServiceInstaller()
        {
            //Instantiate and configure a ServiceProcessInstaller
            ServiceProcessInstaller PollingService = new ServiceProcessInstaller();
            PollingService.Account = ServiceAccount.LocalSystem;
    
            //Instantiate and configure a ServiceInstaller
            ServiceInstaller PollingInstaller = new ServiceInstaller();
            PollingInstaller.DisplayName = "SMMD Polling Service Beta";
            PollingInstaller.ServiceName = "SMMD Polling Service Beta";
            PollingInstaller.StartType = ServiceStartMode.Automatic;
    
            //Add both the service process installer and the service installer to the
            //Installers collection, which is inherited from the Installer base class.
            Installers.Add(PollingInstaller);
            Installers.Add(PollingService);
        }
    }
    

Finally you'll use a command line utility to actually install the service - you can read about how that works here:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d8f300e3-6c09-424f-829e-c5fda34c1bc7

Let me know if you have any questions.

Koen
  • 3,626
  • 1
  • 34
  • 55
Aaronontheweb
  • 8,224
  • 6
  • 32
  • 61
2

There is the Open Source Framework that host .net application as Windows service. There are no pain installing, uninstalling windows service. It decouples very well. Please check this post Topshelf Windows Service Framework Post

Community
  • 1
  • 1
Amzath
  • 3,159
  • 10
  • 31
  • 43
0

I want to show easly way to run service

At first you want if your service isn't stopped or something other status:

public static bool isServiceRunning(string serviceName)
    {
        ServiceController sc = new ServiceController(serviceName);
        if (sc.Status == ServiceControllerStatus.Running)
            return true;
        return false;
    }

And next if service is not running you go to use this simple method

public static void runService(string serviceName)
    {
        ServiceController sc = new ServiceController(serviceName);
        sc.Start();
    }
BlueFox
  • 17
  • 5