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
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?
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://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services
Short answer: recreate your project using the "Windows service" template as described here:
https://docs.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://docs.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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.