0

I have a windows service application which i build with c# in visual studio. Basically the application is fetching data from a API service and saving into an another software installed on my machine using SDK. The application is working fine but it runs under the background processes of the windows. But i want it to run in the services

enter image description here

Here is my program.cs main() code

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        Service1 myService = new Service1();
        myService.OnDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }

    
}

What i can change here to run it under the windows services?

Harish Kumar
  • 927
  • 3
  • 20
  • 46

2 Answers2

0

You need to register your .exe in your services.

you can do it running this line in your powershell:

New-Service -Name "YourServiceName" -BinaryPathName <yourproject>.exe

For more details: https://learn.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services

Jose Cruz
  • 11
  • 3
  • I'm sure, if you created a windows service application, It should work. You don't need to believe in me, just check the documentation: https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer – Jose Cruz Aug 25 '20 at 14:52
0

Short answer: recreate your project using the "Windows service" template as described here:

https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

then install it using installutil or using the integrated installer

Longer answer:

A service is a "Console application" with specific entry points, so this is the very basic code to create the service:

using System.ServiceProcess;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            this.ServiceName = "Service1";
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
}

as you can see, the main class inherits and implements the "ServiceBase" class and override a few methods. The main methods are "OnStart" (called when you start a service) and "OnStop", called when you stop it.

There are a lot of other properties and methods, described here (or pressing F12 in visual studio on the class name):

https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase

Taking a look at the "main", you can see how it works:

    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new Service1()
        };
        ServiceBase.Run(ServicesToRun);
    }

a few things you have to remember:

  • a service is executed in c:\windows\system32 as base path. Do not use relative paths.

  • OnStart has to be quick. Do not perform long operations in that method. Best course of action is perform all start checks and Launch a thread.

  • change the main in this way to allow debugging (obviously TestMode should be the code to test):

    bool isInteractive = Environment.UserInteractive || args.Contains("--interactive");

    if (isInteractive) ((Service1)ServicesToRun[0]).TestMode(); else ServiceBase.Run(ServicesToRun);

  • Once built you .exe file, use installutil to install it as a service

    Install a Windows service using a Windows command prompt?

AndreaCi
  • 809
  • 1
  • 9
  • 22